提交 b1899ea1 编写于 作者: GreyZeng's avatar GreyZeng

leetcode 260 single number iii

上级 364dcc11
......@@ -4,6 +4,8 @@ package git.snippet.bit;
// which appears exactly once. Find the single element and return it.
// You must implement a solution with a linear runtime complexity and use only constant extra space.
// https://www.cnblogs.com/greyzeng/p/15385402.html
// NowCoder_FindOneInK 的特例之一而已
// leetcode: https://leetcode.com/problems/single-number-ii/
public class LeetCode_0137_SingleNumberII {
public int singleNumber(int[] nums) {
int[] bit = new int[32];
......
......@@ -4,20 +4,22 @@ package git.snippet.bit;
// https://www.cnblogs.com/greyzeng/p/15385402.html
// https://leetcode.com/problems/single-number-iii/
public class LeetCode_0260_SingleNumberIII {
public int[] singleNumber(int[] nums) {
int eor = nums[0];
for (int i = 1; i < nums.length; i++) {
eor ^= nums[i];
int eor = 0;
for (int num : nums) {
eor ^= num;
}
// eor = a ^ b;
int leftOne = eor & (-eor);
// eor = a ^ b
// rightOne = (eor) & (-eor)
int rightOne =(eor) & ((~eor) + 1);
int a = 0;
for (int i = 0; i < nums.length; i++) {
if ((nums[i] & leftOne) == 0) {
a ^= nums[i];
}
for (int num : nums) {
if ((num & rightOne) == 0) {
a ^= num;
}
}
int b = a ^ eor;
return new int[]{a, b};
return new int[] {a, b};
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册