文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
2. Solution
- Simple Method
class Solution {
public:
int singleNumber(vector<int>& nums) {
sort(nums.begin(), nums.end());
int i = 0;
while(i < nums.size() - 1) {
if(nums[i] == nums[i + 1]) {
i += 3;
}
else {
return nums[i];
}
}
return nums[nums.size() - 1];
}
};
- O(N)
class Solution {
public:
int singleNumber(vector<int>& nums) {
int a = 0;
int b = 0;
for(int i = 0; i < nums.size(); i++) {
a = (a ^ nums[i]) & (~b);
b = (b ^ nums[i]) & (~a);
}
return a;
}
};