-
Notifications
You must be signed in to change notification settings - Fork 0
/
n260SingleNumberIII.java
59 lines (55 loc) · 1.81 KB
/
n260SingleNumberIII.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.*;
/**Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
For example:
Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].
Note:
The order of the result is not important. So in the above example, [5, 3] is also correct.
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
你的算法的时间复杂度应为O(n),你能用空间复杂度O(1)实现吗?
*/
public class n260SingleNumberIII {
public static void main(String[] args) {
int[] nums = {1, 2, 1, 3, 2, 5};
Solution260 a = new Solution260();
a.singleNumber(nums);
}
}
class Solution260 {
// public int[] singleNumber(int[] nums) {
// int[] res = new int[2];
// if(nums.length<=1) return res;
//
// Set<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> it = set.iterator();
// for(int i=0;i<res.length; i++){
// res[i] = it.next();
// }
// return res;
// }
public int[] singleNumber(int[] nums) {
int[] res = new int[2];
int result = nums[0];
for(int i=1;i<nums.length;i++){
result = result^nums[i];
}
res[0] = 0;
res[1] = 0;
int n = result & (~(result-1)); //使用n&(~(n-1))可以取出元素的最后一个1的位置。
for(int i=0;i<nums.length;i++){
if((n & nums[i])!=0){
res[0] = res[0] ^ nums[i];
}else {
res[1] = res[1] ^ nums[i];
}
}
return res;
}
}