class Solution: # @param A, a list of integer # @return an integer def singleNumber(self, A): rst=0 for i in A: rst^=i return rst
Sunday, August 3, 2014
Leetcode: Single Number @Python
Use bitwise XOR. For each bit, odd number of appearance of 0/1 will still be 0/1, even number of appearance of 0/1 will be 0. And since for any number x, 0^x=x, use 0 as the initial value for iteration.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment