-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleNumber.java
More file actions
60 lines (53 loc) · 1.52 KB
/
SingleNumber.java
File metadata and controls
60 lines (53 loc) · 1.52 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
57
58
59
60
package easy;
import java.util.HashMap;
/**
*
* ClassName: SingleNumber
* @author chenyiAlone
* Create Time: 2018/12/29 09:22:12
* Description: No.136
* Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
*/
public class SingleNumber {
// 使用HashMap
// 时间复杂度 O(n)
public int singleNumber(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i : nums) {
if (map.containsKey(i)) {
map.put(i, map.get(i) + 1);
} else {
map.put(i, 1);
}
}
for (int i : nums) {
if (map.get(i) == 1) return i;
}
return -1;
}
// 暴力搜索
// 时间复杂度 O(n^2)
public int singleNumber1(int[] nums) {
int[] size = new int[nums.length];
for (int i = 1; i < nums.length; i++) {
for (int j = i - 1; j >= 0; j--) {
if (nums[i] == nums[j]) {
size[j]++;
size[i]++;
}
}
}
for (int i = 0; i < size.length; i++) {
if (size[i] == 0) return nums[i];
}
return -1;
}
}