leetcode刷题,总结,记录,备忘137

leetcode137

Single Number II

 

Given an array of integers, every element appears three times except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

暂时没想出位操作的方法,,用的map容器,时间复杂度为O(n),,比较简单,,直接上代码,,我去搜搜别人的解法。。。
class Solution {
public:
    int singleNumber(vector<int>& nums) {
      map<int, int> mi;
      for (vector<int>::iterator it = nums.begin(); it != nums.end(); ++it)
      {
          mi[*it]++;
      }
      
      for (map<int, int>::iterator it = mi.begin(); it != mi.end(); ++it)
      {
            if (it->second < 3)
            return it->first;
      }
    }
};

你可能感兴趣的:(leetcode刷题,总结,记录,备忘137)