-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleNumberII_137.java
More file actions
45 lines (41 loc) · 1.05 KB
/
Copy pathSingleNumberII_137.java
File metadata and controls
45 lines (41 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package Bit;
import java.util.HashMap;
import java.util.Map;
public class SingleNumberII_137 {
/**
* 时间复杂度为O(n) 空间O(n)
*/
public int singleNumber(int[] nums) {
Map<Integer,Integer> map=new HashMap<>();
for(int num:nums){
map.put(num,map.getOrDefault(num,0)+1);
}
int res=0;
for(int key:map.keySet()){
if(map.get(key)==1){
res=key;
break;
}
}
return res;
}
/**
* 时间O(nlogC) 其中C为数据范围 空间O(1)
* @param nums
* @return
*/
public int singleNumberSecond(int[] nums) {
//结果二进制形式上第i位上的值=所有num第i位上值的和对3取余的结果,因为其余数字出现3次
int res=0;
for(int i=0;i<32;i++){
int total=0;
for(int num:nums){
total+=((num>>i)&1);
}
if(total%3!=0){
res|=(1<<i);
}
}
return res;
}
}