-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleNumber.java
More file actions
33 lines (30 loc) · 1.08 KB
/
SingleNumber.java
File metadata and controls
33 lines (30 loc) · 1.08 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
package com.leetcode.bits;
public class SingleNumber {
/*
定一个整数数组nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。你可以按 任意顺序 返回答案。
进阶:你的算法应该具有线性时间复杂度。你能否仅使用常数空间复杂度来实现?
链接:https://leetcode-cn.com/problems/single-number-iii
*/
public int[] singleNumber(int[] nums) {
int bitmask = 0;
for (int num : nums) {
bitmask ^= num;
}
//bitmask &= -bitmask,保留最低位的1
bitmask &= -bitmask;
int[] ret = {0, 0};
for (int num : nums) {
if ((bitmask & num) != 0) {
ret[0] ^= num;
} else {
ret[1] ^= num;
}
}
return ret;
}
public static void main(String[] args) {
System.out.println(Integer.toBinaryString(32));
System.out.println(Integer.toBinaryString(-32));
System.out.println(32&-32);
}
}