260:只出现一次的数字 III

This commit is contained in:
huangge1199@hotmail.com 2021-07-18 19:16:57 +08:00
parent 04ecc22559
commit c26668abd4

View File

@ -39,19 +39,34 @@
// 👍 416 👎 0 // 👍 416 👎 0
package leetcode.editor.cn; package leetcode.editor.cn;
//260:只出现一次的数字 III //260:只出现一次的数字 III
class SingleNumberIii{ class SingleNumberIii {
public static void main(String[] args) { public static void main(String[] args) {
//测试代码 //测试代码
Solution solution = new SingleNumberIii().new Solution(); Solution solution = new SingleNumberIii().new Solution();
} }
//力扣代码 //力扣代码
//leetcode submit region begin(Prohibit modification and deletion) //leetcode submit region begin(Prohibit modification and deletion)
class Solution { class Solution {
public int[] singleNumber(int[] nums) { public int[] singleNumber(int[] nums) {
return null; int xor = 0;
for (int i = 0; i < nums.length; i++) {
xor ^= nums[i];
}
xor &= -xor;
int[] result = new int[2];
for (int num : nums) {
if ((num & xor) == 0) {
result[0] ^= num;
} else {
result[1] ^= num;
}
}
return result;
}
} }
}
//leetcode submit region end(Prohibit modification and deletion) //leetcode submit region end(Prohibit modification and deletion)
} }