-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path260_SingleNumberIII.py
34 lines (30 loc) · 1.09 KB
/
260_SingleNumberIII.py
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: [email protected]
class Solution(object):
# Clear explanation according to
# https://leetcode.com/discuss/60408/sharing-explanation-of-the-solution
def singleNumber(self, nums):
xor_res = 0
for num in nums:
xor_res ^= num
# Assume the two different numbers diff at ith bit(i is the rightmost).
# Then we can get 0x000...1...000, 1 is the ith bit by the following.
xor_res &= -xor_res
num_a, num_b = 0, 0
for num in nums:
# All the numbers can be partitioned into
# two groups according to their bits at location i.
# The first group consists of all numbers whose bits at i is 0.
# The second group consists of all numbers whose bits at i is 1.
# The two different number a and b is in the two different groups.
if num & xor_res == 0:
num_a ^= num
else:
num_b ^= num
return [num_a, num_b]
"""
[-1,0]
[1, 2, 1, 3, 2, 5]
[-1,-1,-2,-2,-3,-3,-3,-3,4,-5]
"""