forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT2.java
More file actions
56 lines (52 loc) · 1.14 KB
/
T2.java
File metadata and controls
56 lines (52 loc) · 1.14 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
46
47
48
49
50
51
52
53
54
55
56
/**
* @program JavaBooks
* @description: 只出现一次的数字
* @author: mf
* @create: 2020/04/10 20:34
*/
package subject.hash;
import java.util.HashMap;
import java.util.Map;
/**
* 输入: [2,2,1]
* 输出: 1
* 输入: [4,1,2,1,2]
* 输出: 4
*/
public class T2 {
/**
* 亦或
* @param nums
* @return
*/
public int singleNumber(int[] nums) {
if (nums.length == 1) return nums[0];
int ans = nums[0];
for (int i = 1; i < ans; i++) {
ans ^= nums[i];
}
return ans;
}
/**
* 哈希
* @param nums
* @return
*/
public int singleNumber2(int[] nums) {
if (nums.length == 1) return nums[0];
HashMap<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
if (map.containsKey(num)) {
map.put(num, map.get(num) + 1);
} else {
map.put(num, 1);
}
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getKey() == 1) {
return entry.getKey();
}
}
return -1;
}
}