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

浙公网安备 33010602011771号