-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathSingleNumber136.java
44 lines (40 loc) · 1.16 KB
/
SingleNumber136.java
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
/**
* Given an 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?
*/
public class SingleNumber136 {
public int singleNumber(int[] nums) {
Arrays.sort(nums);
for (int i=0; i<nums.length; i++) {
int n = nums[i];
if ((i > 0 && n == nums[i-1]) || (i < nums.length-1 && n == nums[i+1])) continue;
return nums[i];
}
return -1;
}
public int singleNumber2(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int i=0; i<nums.length; i++) {
if (set.contains(nums[i])) {
set.remove(nums[i]);
} else {
set.add(nums[i]);
}
}
return set.iterator().next();
}
/**
* https://leetcode.com/problems/single-number/discuss/42997/My-O(n)-solution-using-XOR
*/
public int singleNumber3(int[] nums) {
int result = 0;
for (int i = 0; i<nums.length; i++) {
result ^= nums[i];
}
return result;
}
}