-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
357 lines (347 loc) · 10.1 KB
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
class Solution:
# @param {integer} n
# @return {integer[]}
def grayCode(self, n):
if n < 1: return [0,]
if n == 1: return [0,1]
pre_bit = 1<<(n-1)
lower = self.grayCode(n-1)
return lower + [(pre_bit|i) for i in reversed(lower)]
def isIsomorphic(self, s, t):
if len(s) != len(t):
return False
if len(s) == 0:
return True
dict1 = {s[0]:t[0],}
dict2 = {t[0]:s[0],}
for i in range(1,len(s)):
if s[i] in dict1:
vi = dict1[s[i]]
if vi != t[i]:
return False
else:
if t[i] in dict2:
return False
dict1[s[i]] = t[i]
dict2[t[i]] = s[i]
return True
def permuteUnique(self, nums):
if nums==None:
return None
if len(nums)==0:
return None
if len(nums)==1:
return [nums,]
first=nums[0]
result=[]
reduce_nums=nums[1:]
perm_result = self.permuteUnique(reduce_nums)
for i in range(0,len(perm_result)):
for j in range(0, len(perm_result[i])+1):
if (j-1>=0)&(first==perm_result[i][j-1]):
continue
perm=[it for it in perm_result[i]]
perm.insert(j,first)
if perm not in result:
result.append(perm)
return result
def removeSpaces(self,s):
n = len(s)
i = 0
j = n-1
while i < n and s[i]==' ':
i+=1
while j > -1 and s[j]==' ' :
j-=1
return s[i:j+1]
def isNumber(self, s):
s = self.removeSpaces(s)
n = len(s)
if n == 0:
return False
i = 0
dotFlag = False
EFlag = False
hasDigit = False
hasSign = False
while i < n:
if s[i].isdigit():
i+=1
hasDigit = True
hasSign = True
elif not dotFlag and s[i]=='.':
i+=1
dotFlag = True
hasSign = True
elif hasDigit and not EFlag and (s[i]=='e' or s[i]=='E'):
i+=1
dotFlag = True
EFlag = True
hasDigit = False
hasSign = False
elif not hasDigit and not hasSign and (s[i]=='+' or s[i]=='-'):
i+=1
hasSign = True
else :
return False
if not hasDigit:
return False
else :
return True
def searchInsert(self, nums, target):
n = len(nums)
m = n/2
if n==0:
return 0
if n==1:
if nums[0]>=target:
return 0
else:
return 1
if nums[m]==target:
return m
elif nums[m]>target:
return self.searchInsert(nums[0:m],target)
else:
if (m+1)==n:
return n
else:
return (m+1)+self.searchInsert(nums[m+1:n],target)
def numDistinct(self, s, t):
m = len(t)
n = len(s)
#init mem matrix
mem = []
for i in range(0,m+1):
row = []
for j in range(0,n+1):
row.append(0)
mem.append(row)
#set first line
for j in range(0,n+1):
mem[0][j] = 1
for i in range(0,m):
for j in range(0,n):
if s[j]==t[i]:
mem[i+1][j+1] = mem[i+1][j] + mem[i][j]
else:
mem[i+1][j+1] = mem[i+1][j]
return mem[m][n]
def level(self, n):
m = n
l = 0
while(m>1):
m = m/2
l+=1
return l
def hammingWeight(self, n):
if n == 0:
return 0
hw = 0
while (n != 0) :
if n&1 == 1:
hw+=1
n=n>>1
return hw
def numTrees(self, n):
if n == 0 :
return 0
if n == 1:
return 1
c = [0,1]
for i in range(2,n+1):
count_ci = 0
for j in range(0,i):
l = j
r = i-j-1
if c[l]!=0 and c[r]!=0:
count_ci += c[l]*c[r]
else:
count_ci += c[l]+c[r]
c.append(count_ci)
return c[n]
def canFinish(self, numCourses, prerequisites):
graph = [[] for _ in range(0,numCourses)]
visit = [0 for _ in range(0, numCourses)]
for prr in prerequisites:
graph[prr[0]].append(prr[1])
def dfs(i):
if visit[i]==-1:
return False
elif visit[i]==1:
return True
visit[i]=-1
for j in graph[i]:
if dfs(j)== False:
return False
visit[i]=1
return True
for i in range(0,numCourses):
if dfs(i)==False:
return False
return True
def findOrder(self, numCourses, prerequisites):
graph = [[] for _ in xrange(numCourses)]
visit = [0 for _ in xrange(numCourses)]
for x,y in prerequisites:
graph[x].append(y)
order = []
def dfs(i):
if visit[i]==-1:
return None
if visit[i]==1:
return order
visit[i]=-1
for j in graph[i]:
r = dfs(j)
if r == None:
return None
visit[i]=1
order.append(i)
return order
for i in xrange(numCourses):
r = dfs(i)
if r == None:
return []
return order
def sortedArrayToBST(self, nums):
n = len(nums)
if n == 0 :
return None
if n == 1:
return TreeNode(nums[0])
m = n/2
left_nums = nums[0:m]
right_nums = nums[m+1:n]
left_node = self.sortedArrayToBST(left_nums)
right_node = self.sortedArrayToBST(right_nums)
node = TreeNode(nums[m])
node.left = left_node
node.right = right_node
return node
def sortedListToBST(self, head):
if head == None:
return None
arr = []
while (head != None):
arr.append(head.val)
head = head.next
return self.sortedArrayToBST(arr)
def sLToBST(self,head, n):
if head==None:
return None
if n == 0:
return None
if n == 1:
return TreeNode(head.val)
m = n/2
i = 0
p = head
while i<m:
p = p.next
i+=1
node = TreeNode(p.val)
node.left = self.sLToBST(head,m)
node.right = self.sLToBST(p.next,n-m-1)
return node
def twoSum(self, nums, target):
n = len(nums)
dict = {}
for i in xrange(n):
t = target - nums[i]
if t in dict:
return [dict[t]+1,i+1]
dict[nums[i]] = i
return []
#check the value in i, j is valid
def isValidSudoku(self, board,i,j):
v = board[i][j]
if v == '.':
return True
if int(v)>9:
return False
for n in xrange(9):
c_hor = board[i][n]
if c_hor == v and n != j:
return False
c_ver = board[n][j]
if c_ver == v and n != i:
return False
for n in xrange(3):
for m in xrange(3):
i_hor = (i/3)*3+n
j_ver = (j/3)*3+m
c_temple = board[i_hor][j_ver]
if c_temple == v and (i !=i_hor or j !=j_ver):
return False
return True
# @param {character[][]} board
# @return {void} Do not return anything, modify board in-place instead.
def solveSudoku(self, board):
indexes = []
for i in xrange(9):
for j in xrange(9):
if board[i][j] == '.':
index= i*9+j
indexes.append(index)
l = len(indexes)
p = 0
while (p >= 0 and p < l):
index = indexes[p]
i = index/9
j = index%9
if board[i][j] == '.':
# fill new
for q in range(1,10):
board[i][j] = str(q)
if self.isValidSudoku(board, i,j) == True:
break
else :
board[i][j] = '.'
if board[i][j] == '.':
p-=1 # back trace
else:
p+=1 # next
else:
if board[i][j]=='9':
board[i][j] = '.'
p -= 1
continue
#else
board[i][j] = str(int(board[i][j]) + 1)
while(self.isValidSudoku(board,i,j)==False):
if board[i][j] == '9':
break
else:
board[i][j] = str(int(board[i][j])+1)
if self.isValidSudoku(board, i, j):
p +=1 #next
else:
board[i][j] = '.'
p -=1 # back trace
# zig zag
# @param {string} s
# @param {integer} numRows
# @return {string}
def convert(self, s, numRows):
if numRows == 1:
return s
rows = []
for i in xrange(numRows):
rows.append([])
i = 0
plus = True
for j in xrange(len(s)):
rows[i].append(s[j])
if i == 0:
plus = True
elif i == (numRows-1):
plus = False
if plus:
i += 1
else:
i -= 1
new_s = ''
for i in xrange(numRows):
new_s += ''.join(rows[i])
return new_s