1.136题
https://leetcode-cn.com/problems/single-number/
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res = 0;
for (auto x: nums) res ^= x;
return res;
}
};
2.https://leetcode-cn.com/problems/single-number-iii/ 260题
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int ab=0;
for(auto x:nums) ab=ab^x; //a^b
int k=0;
while((ab>>k&1)==0) k++;
int a=0;
for(auto x:nums)
{
if(x>>k&1) a=a^x;
}
return {a,ab^a};
}
};
3.https://leetcode-cn.com/problems/single-number-ii/
137
class Solution {
public:
int singleNumber(vector<int>& nums) {
int one=0,two=0;
for(auto x:nums)
{
one=(one^x)&(~two);
two=(two^x)&(~one);
}
return one;
}
};