题目描述
blablabla
样例
blablabla
算法1
(一次遍历) $O(n)$
blablabla
时间复杂度分析:0(n)
C++ 代码
class Solution {
public:
int findNumberAppearingOnce(vector<int>& nums) {
int length = nums.size();
if (length == 0) return -1;
int bitsum[32] = {0};
for (int i = 0; i < length; i++){
int bitmask = 1;
for (int j = 31; j >= 0; j-- ){
bitsum[j] += (nums[i] & bitmask)?1 : 0;
bitmask = bitmask << 1;
}
}
int result = 0;
for (int i = 0; i < 32; i++) {
result = result << 1;
result += bitsum[i]%3;
//result = result << 1;
}
return result;
}
};