Saturday, August 22, 2015

Leetcode 137. Single Number II

https://leetcode.com/problems/single-number-ii/

Now this is much more complex. No simple operation to solve.

The solution is actually using binary to simulate ternary.

Solution:
# T:O(n) S:O(1)
lass Solution:
    # @param A, a list of integer
    # @return an integer
    def singleNumber(self, A):
        one, two = 0, 0
        for x in A:
            one, two = (~x & one) | (x & ~one & ~two), (~x & two) | (x & one)
        return one
Run Time: 64 ms

No comments:

Post a Comment