Created
May 17, 2014 03:45
-
-
Save tomkaith13/8a63947141fe7790b27c to your computer and use it in GitHub Desktop.
leetcode - single number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Given an array of integers, every element appears twice except for one. Find that single one. | |
''' | |
class Solution: | |
# @param A, a list of integer | |
# @return an integer | |
def singleNumber(self, A): | |
A.sort() | |
x=0 | |
y=1 | |
if len(A) == 1: | |
return A[0] | |
else: | |
for i in xrange(len(A) / 2): | |
#print x,y,i | |
if A[x] != A[y]: | |
return A[x] | |
break | |
x = x+2 | |
y = y+2 | |
else: | |
return A[-1] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment