Leetcode string two numbers that appear only once in the array

THE

problem

Insert picture description here

solution

Code


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;
        }
    }
   
};

Summary and reflection

  1. The title of this question feels a bit problematic. It has been changed to be greater than or equal to 2, and the four 1s given by the test are considered duplicates.

Guess you like

Origin blog.csdn.net/liupeng19970119/article/details/114176066