forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT40.java
More file actions
39 lines (37 loc) · 1.2 KB
/
Copy pathT40.java
File metadata and controls
39 lines (37 loc) · 1.2 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
package web; /**
* @program LeetNiu
* @description: 数组中只出现一次的数字
* @author: mf
* @create: 2020/01/15 10:27
*/
import java.util.HashMap;
import java.util.Map;
/**
* 一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
*/
public class T40 {
public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
if (array == null || array.length <= 1) {
num1[0] = num2[0] = 0;
return;
}
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < array.length; i++) {
if (map.containsKey(array[i])) {
map.put(array[i], 2);
} else {
map.put(array[i], 1);
}
}
StringBuffer sb = new StringBuffer();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() == 1) {
sb.append(entry.getKey());
sb.append(",");
}
}
String[] strings = sb.toString().split(",");
num1[0] = Integer.valueOf(strings[0]);
num2[0] = Integer.valueOf(strings[1]);
}
}