Skip to content

Instantly share code, notes, and snippets.

@ta1hia
Created March 10, 2016 22:09
Show Gist options
  • Save ta1hia/310e15fc39192bf61652 to your computer and use it in GitHub Desktop.
Save ta1hia/310e15fc39192bf61652 to your computer and use it in GitHub Desktop.
# you can write to stdout for debugging purposes, e.g.
# print "this is a debug message"
def solutionA(A): # 100% but space complexity is not O(1)
L = len(A)
d = {}
if not L: return
for e in A:
if d.has_key(e):
d.pop(e)
else:
d[e] = True
return d.keys()[0]
def solutionB(A): # 77% (detected O(n^2) run time?? but space complexity O(1) due to quicksort
# write your code in Python 2.7
L = len(A)
if L == 1:
return A[0]
qsort(A, 0, L)
while i < L -1:
while i < L-1 and A[i] == A[i+1]:
count += 1
i += 1
if count % 2 == 0:
count = 1
i += 1
else:
break
return A[i]
def qsort(A, L, U):
if L < U:
P = A[L]
W = L
for i in range(L, U):
if A[i] < P:
W += 1
tmp = A[i]
A[i] = A[W]
A[W] = tmp
A[L] = A[W]
A[W] = P
qsort(A, L, W)
qsort(A, W+1, U)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment