要求:三次和一次,找出一次的数,空间O(1)时间O(n)
思路:时间O(32n)
class Solution {
public:
int singleNumber(vector<int>& nums) {
int ans = 0;
for (int i = 0; i < 32; ++i) {
int total = 0;
for (int num: nums) {
total += ((num >> i) & 1);
}
if (total % 3) {
ans |= (1 << i);//相当于加法
}
}
return ans;
}
};