forked from JadeZYX/Java_LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP0136SingleNumber.java
More file actions
63 lines (62 loc) · 1.68 KB
/
Copy pathP0136SingleNumber.java
File metadata and controls
63 lines (62 loc) · 1.68 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
61
62
63
import java.util.Arrays;
import java.util.HashSet;
public class P0136SingleNumber {
public int singgleNumber0(int[]nums){
Arrays.sort(nums);//排序
int i=0;
while(i<nums.length-1){
if(nums[i]!=nums[i+1]){
return nums[i];
}
else{
i=i+2;
}
}
return nums[i];
}
public int singgleNumber(int[]nums){
int ans=0;
for(int n:nums){
ans^=n;
}
return ans;
}
public int singleNumber(int[] nums) {//用set做
HashSet<Integer>set=new HashSet<>();
/*
for(int i:nums){
if(set.contains(i)){
set.remove(i);
}
else{
set.add(i);
}
}
*/
for(int i=0;i<nums.length;i++){
if(set.contains(nums[i])){
set.remove(nums[i]);
}
else{
set.add(nums[i]);
}
}
/*
Iterator<Integer>ite=set.iterator();
while(ite.hasNext()){
return ite.next();
}
*/
for(int n:set){
return n;
}
return -1;
}
//XOR的意思是位异或运算。相同为0,不同为1,所以数组里的数不断的进行位异或,相同的抵消,最后结果就是唯一出现的无法抵消的数。
}
/*
P0136SingleNumber p136=new P0136SingleNumber();
System.out.println(p136.singgleNumber(new int[]{2,2,1}));
System.out.println(p136.singgleNumber(new int[]{4,1,2,1,2}));
System.out.println(p136.singgleNumber(new int[]{1}));
*/