-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode136.java
More file actions
46 lines (40 loc) · 1.22 KB
/
LeetCode136.java
File metadata and controls
46 lines (40 loc) · 1.22 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
import java.util.HashSet;
import java.util.Iterator;
public class LeetCode136 {
public static void main(String[] args) {
// 输入:nums = [2,2,1]
// 输出:1
System.out.println(new Solution136_2().singleNumber(new int[] { 2, 2, 1 }));
// 输入:nums = [4,1,2,1,2]
// 输出:4
System.out.println(new Solution136_2().singleNumber(new int[] { 4, 1, 2, 1, 2 }));
// 输入:nums = [1]
// 输出:1
System.out.println(new Solution136_2().singleNumber(new int[] { 1 }));
}
}
class Solution136_1 {
public int singleNumber(int[] nums) {
HashSet<Integer> set = new HashSet<Integer>();
for (int i = 0; i < nums.length; i++) {
if (set.contains(nums[i])) {
set.remove(nums[i]);
} else {
set.add(nums[i]);
}
}
Iterator<Integer> iterator = set.iterator();
return iterator.next();
}
}
class Solution136_2 {
public int singleNumber(int[] nums) {
int ans = nums[0];
if (nums.length > 1) {
for (int i = 1; i < nums.length; i++) {
ans = ans ^ nums[i];
}
}
return ans;
}
}