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