Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:Your algorithm should have a linear run time complexity. Could you implement it without using extra memory?
class Solution {public: int singleNumber(vector<int>& nums) { int r = 0; int len = nums.size(); for(int i=0;i<32;i++){ int count = 0; int mask = (1<<i); for(int n=0;n<len;n++){ if(nums[n] & mask){ count += 1; } } if(count%3){ r |= mask; } } return r; }};
新闻热点
疑难解答