https://leetcode.com/problems/single-number/
Given an array of integers, every element appears twice 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) {
if(nums.size() == ) return nums[]; vector<int> bitNums(, ); int res = ;
for(int i=;i<bitNums.size();++i) {
for(int j=;j<nums.size();++j) {
bitNums[i] += (nums[j] >> i) & ;
}
res |= (bitNums[i] % ) << i;
} return res;
}
};
https://leetcode.com/problems/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) {
if(nums.size() == ) return nums[]; vector<int> bitNums(, ); int res = ;
for(int i=;i<;++i) {
for(int j=;j<nums.size();++j) {
bitNums[i] += (nums[j] >> i) & ;
cout << ((nums[j] >> i) & ) << " ";
}
cout<<endl;
} for(int i=;i<bitNums.size();++i) {
res += (bitNums[i] % ) << i;
}
return res;
}
};