2、Single Number

关键:Hash

unordered_map<int, int> ::iterator it = h.begin();//迭代器

哈希表的遍历

次数为Value,值为key

复杂度为O(n)

 1 class Solution {
 2 public:
 3     int singleNumber(vector<int>& nums) {
 4         unordered_map<int,int> h;
 5         for(int i=0; i<nums.size(); i++){
 6             h[nums[i]] = h[nums[i]] + 1;
 7         }
 8 
 9         int res;
10         unordered_map<int,int>::iterator it = h.begin();
11         for( ; it!= h.end(); it++){
12             if(it->second == 1)res = it->first;
13         }
14         return res;
15     }
16 };

 

 

posted @ 2020-07-05 15:04  Gumpest  阅读(62)  评论(0)    收藏  举报