Open azl397985856 opened 1 month ago
class Solution: # T: O(n) S: O(1) def singleNumber(self, nums: List[int]) -> List[int]:
xor = 0
for n in nums:
xor ^= n
# find the first diff bit and assgin numbers into two groups based on the diff bit
diff_bit = 1
while not (xor & diff_bit):
diff_bit = diff_bit << 1
a, b = 0, 0
for n in nums:
if diff_bit & n:
a ^= n
else:
b ^= n
return [a,b]
class Solution: def singleNumber(self, nums: List[int]) -> List[int]: xor = a = b = 0 right_bit = 1 length = len(nums) for i in nums: xor ^= i while right_bit & xor == 0: right_bit <<= 1 for i in nums: if right_bit & i: a ^= i else: b ^= i return [a, b]
260. 只出现一次的数字 III
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/single-number-iii/
前置知识
题目描述
示例 :
输入: [1,2,1,3,2,5] 输出: [3,5] 注意:
结果输出的顺序并不重要,对于上面的例子, [5, 3] 也是正确答案。 你的算法应该具有线性时间复杂度。你能否仅使用常数空间复杂度来实现?