|
|
@ -232,3 +232,56 @@ class Solution { |
|
|
|
} |
|
|
|
} |
|
|
|
``` |
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[56 - II. 数组中数字出现的次数 II](https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof/) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```java |
|
|
|
|
|
|
|
class Solution { |
|
|
|
|
|
|
|
public int singleNumber(int[] nums) { |
|
|
|
|
|
|
|
Arrays.sort(nums); |
|
|
|
|
|
|
|
for (int i = 0; i < nums.length - 2; i += 3) { |
|
|
|
|
|
|
|
if (nums[i] != nums[i + 2]) { |
|
|
|
|
|
|
|
return nums[i]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return nums[nums.length - 1]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```java |
|
|
|
|
|
|
|
class Solution { |
|
|
|
|
|
|
|
public int singleNumber(int[] nums) { |
|
|
|
|
|
|
|
Map<Integer, Integer> map = new HashMap<>(); |
|
|
|
|
|
|
|
for (int i : nums) { |
|
|
|
|
|
|
|
int count = map.getOrDefault(i, 0); |
|
|
|
|
|
|
|
map.put(i, count + 1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (Integer i : map.keySet()) { |
|
|
|
|
|
|
|
if (map.get(i) == 1) { |
|
|
|
|
|
|
|
return i; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return -1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```java |
|
|
|
|
|
|
|
class Solution { |
|
|
|
|
|
|
|
public int singleNumber(int[] nums) { |
|
|
|
|
|
|
|
int[] count = new int[32]; |
|
|
|
|
|
|
|
for (int i : nums) { |
|
|
|
|
|
|
|
for (int j = 0; j < 32; j++) { |
|
|
|
|
|
|
|
count[j] += i & 1; |
|
|
|
|
|
|
|
i = i >>> 1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
int res = 0, flag = 3; |
|
|
|
|
|
|
|
for (int i = 0; i < 32; i++) { |
|
|
|
|
|
|
|
res += (1 << i) * (count[i] % flag); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return res; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|