leetcode string 数组中只出现一次的两个数

O

问题

在这里插入图片描述

解决方案

代码


class Solution {
    
    
public:
   /**
    * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
    *
    * 
    * @param array int整型vector 
    * @return int整型vector
    */
   
/*
思路: 采用哈希表存储数据。 遍历之后如果不等于2 , 找出来并排序。

- 
- 
- - 
- - 
- 
*/
   vector<int> FindNumsAppearOnce(vector<int>& array) {
    
    
       // write code here
       unordered_map<int, int> need;
       vector<int> ans;
       for(int i = 0; i< array.size();i++){
    
    
           need[array[i]]++;
       }

       for (unordered_map<int, int>::iterator it = need.begin(); it != need.end(); it++) {
    
      //用迭代器遍历容器
           if((*it).second < 2 ) ans. push_back((*it).first);
       }
       compare(ans);
       return ans;
   }
    void compare(vector<int>& ans){
    
    
        if(ans[0]>ans[1]){
    
    
            int temp = ans[0];
            ans[0] = ans[1];
            ans[1] = temp;
        }
    }
   
};

总结与反思

  1. 这道题的题目感觉有点问题。 已改是大于等于2 , 测试给的四个1也算重复。

猜你喜欢

转载自blog.csdn.net/liupeng19970119/article/details/114176066