Skip to content

Commit eca05c7

Browse files
committed
no message
1 parent 6c6b1c0 commit eca05c7

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

problems/alien-dictionary.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Topological Sort
3+
"""
4+
class Solution(object):
5+
def alienOrder(self, words):
6+
#return true if cycles are detected.
7+
def dfs(c):
8+
if c in path: return True
9+
if c in visited: return False
10+
path.add(c)
11+
for nei in adj[c]:
12+
if dfs(nei): return True
13+
res.append(c)
14+
path.remove(c)
15+
visited.add(c)
16+
return False
17+
18+
#build adjacency list
19+
adj = {c: set() for word in words for c in word}
20+
for i in xrange(len(words)-1):
21+
w1, w2 = words[i], words[i+1]
22+
minLen = min(len(w1), len(w2))
23+
if w1[:minLen]==w2[:minLen] and len(w1)>len(w2): return ""
24+
25+
for j in xrange(minLen):
26+
if w1[j]!=w2[j]:
27+
adj[w1[j]].add(w2[j])
28+
break
29+
30+
#topological sort
31+
path = set() #path currently being reversed
32+
visited = set() #done processing
33+
res = []
34+
for c in adj:
35+
if dfs(c): return ""
36+
37+
return "".join(reversed(res))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def removeNthFromEnd(self, head, n):
3+
def getCount(node0):
4+
curr = node0
5+
count = 0
6+
while curr:
7+
curr = curr.next
8+
count += 1
9+
return count
10+
11+
def removeNext(node0):
12+
nextNode = node0.next
13+
if not nextNode: return
14+
node0.next = nextNode.next
15+
16+
k = getCount(head)-n-1 #need to "curr = curr.next" k times to reach the node that we can call removeNext(curr)
17+
if k==-1: return head.next #remove head
18+
19+
curr = head
20+
while k>0:
21+
k -= 1
22+
curr = curr.next
23+
24+
removeNext(curr)
25+
return head

problems/rotate-image.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def rotate(self, matrix):
3+
N = len(matrix)
4+
5+
#transpose
6+
for i in xrange(N):
7+
for j in xrange(N):
8+
if j<=i: continue
9+
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
10+
11+
#reflect
12+
for i in xrange(N):
13+
for j in xrange(N/2):
14+
matrix[i][j], matrix[i][N-1-j] = matrix[i][N-1-j], matrix[i][j]
15+
16+
return matrix

problems/spiral-matrix.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution(object):
2+
def spiralOrder(self, matrix):
3+
ans = []
4+
i, j = 0, 0
5+
direction = 'right'
6+
count = 0
7+
8+
while count<=len(matrix)*len(matrix[0]):
9+
iNext, jNext = i, j
10+
11+
if direction=='right':
12+
if j+1<len(matrix[0]) and matrix[i][j+1]!='v':
13+
jNext = j+1
14+
else:
15+
direction = 'down'
16+
elif direction=='down':
17+
if i+1<len(matrix) and matrix[i+1][j]!='v':
18+
iNext = i+1
19+
else:
20+
direction = 'left'
21+
elif direction=='left':
22+
if j-1>=0 and matrix[i][j-1]!='v':
23+
jNext = j-1
24+
else:
25+
direction = 'up'
26+
elif direction=='up':
27+
if i-1>=0 and matrix[i-1][j]!='v':
28+
iNext = i-1
29+
else:
30+
direction = 'right'
31+
32+
if (iNext, jNext)!=(i, j) or count>=len(matrix)*len(matrix[0])-1:
33+
ans.append(matrix[i][j])
34+
matrix[i][j] = 'v' #visited
35+
count += 1
36+
i = iNext
37+
j = jNext
38+
39+
return ans[:-1]
40+
41+
42+

0 commit comments

Comments
 (0)