Skip to content

Latest commit

 

History

History
21 lines (19 loc) · 483 Bytes

137.md

File metadata and controls

21 lines (19 loc) · 483 Bytes

137. Single Number II

Solution 1 (time O(n), space O(1))

class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        ans = 0
        for i in range(32):
            cur_sum = sum([(num >> i) & 1 for num in nums])
            if cur_sum % 3:
                if i != 31:
                    ans += (1 << i)
                else:
                    ans -= (1 << i)
        return ans