-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path136Single_Number.java
More file actions
45 lines (38 loc) · 922 Bytes
/
136Single_Number.java
File metadata and controls
45 lines (38 loc) · 922 Bytes
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
//bit manipulation
class Solution {
public int singleNumber(int[] nums) {
int answer = 0;
for(int i : nums){
answer ^= i;
}
return answer;
}
}
//sort
class Solution {
public int singleNumber(int[] nums) {
Arrays.sort(nums);
int i = 0;
while(i<nums.length-2){
if(nums[i] != nums[i+1]){
return nums[i];
}else{
i+=2;
}
}
return nums[nums.length-1];
}
}
//set, no redundant
class Solution {
public int singleNumber(int[] nums) {
HashSet<Integer> hs = new HashSet<Integer>();
int sum = 0;int sums = 0;
for(int i : nums){
hs.add(i);
sum += i;
}
for(int i : hs) sums+=i;
return 2 * sums - sum;
}
}