在一个数组 nums
中除一个数字只出现一次之外,其他数字都出现了三次。请找出那个只出现一次的数字。
class Solution {
public int singleNumber(int[] nums) {
int[] count = new int[32];
for(int num : nums){
for(int i = 0; i < 32;i++){
count[i] += (num & 1);
num >>>= 1;
}
}
int res = 0;
for(int i = 0; i < 32;i++){
res <<= 1;
res |= (count[31-i]%3);
}
return res;
}
// public int singleNumber(int[] nums) {
// HashMap<Integer,Integer> map = new HashMap<>();
// for(int num : nums){
// map.put(num,map.getOrDefault(num,0)+1);
// }
// for(int num : nums){
// if(map.get(num) == 1){
// return num;
// }
// }
// return -1;
// }
}
执行用时:3 ms, 在所有 Java 提交中击败了83.93%的用户
内存消耗:43.1 MB, 在所有 Java 提交中击败了5.11%的用户
通过测试用例:32 / 32