forked from qiurunze123/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lc136.java
40 lines (39 loc) · 1.01 KB
/
lc136.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
package code;
/*
* 136. Single Number
* 题意:找出数组中单一的数
* 难度:Easy
* 分类:Hash Table, Bit Maniputation
* 思路:两种巧妙方法:1.异或 2. set以后sum*2 - sum
* Tips:
*/
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class lc136 {
public static void main(String[] args) {
int[] nums = {2,2,1};
System.out.println(singleNumber(nums));
System.out.println(singleNumber2(nums));
}
public static int singleNumber(int[] nums) {
int res = nums[0];
for (int i = 1; i < nums.length ; i++) {
res = res ^ nums[i];
}
return res;
}
public static int singleNumber2(int[] nums) {
Set<Integer> s = new HashSet();
int sum = 0;
int sum2 = 0;
for (int i = 0; i < nums.length ; i++) {
sum += nums[i];
s.add(nums[i]);
}
for(Integer i : s){
sum2 += i;
}
return 2*sum2 - sum;
}
}