Skip to content

Commit 3265fbd

Browse files
authored
Merge pull request algorithm004-01#411 from Ryanyanglibin/master
091-week 02
2 parents 8ec437f + d5da579 commit 3265fbd

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed

Week 02/id_091/LeetCode_17_091.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def letterCombinations(self, digits):
6+
if not digits:
7+
return []
8+
dic = {"2": "abc", "3": "def", "4": "ghi", "5": "jkl", "6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz"}
9+
res = []
10+
self.dfs(digits, dic, 0, "", res)
11+
return res
12+
13+
def dfs(self, digits, dic, index, path, res):
14+
if len(path) == len(digits):
15+
res.append(path)
16+
return
17+
for i in range(index, len(digits)):
18+
for j in dic[digits[i]]:
19+
self.dfs(digits, dic, i + 1, path + j, res)
20+
21+
def letterCombinations2(self, digits: str) -> List[str]:
22+
m = {
23+
'2': list('abc'),
24+
'3': list('def'),
25+
'4': list('ghi'),
26+
'5': list('jkl'),
27+
'6': list('mno'),
28+
'7': list('pqrs'),
29+
'8': list('tuv'),
30+
'9': list('wxyz'),
31+
}
32+
if not digits: return []
33+
ls1 = ['']
34+
for i in digits:
35+
ls1 = [x + y for x in ls1 for y in m[i]]
36+
return ls1
37+
38+
39+
if __name__ == '__main__':
40+
n = "23"
41+
solution = Solution()
42+
res = solution.letterCombinations(n)
43+
print(res)

Week 02/id_091/LeetCode_242_091.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
dic1, dic2 = {}, {}
4+
for item in s:
5+
dic1[item] = dic1.get(item, 0) + 1
6+
for item in t:
7+
dic2[item] = dic2.get(item, 0) + 1
8+
return dic1 == dic2
9+
10+
11+
def isAnagram2(self, s, t):
12+
dic1, dic2 = [0] * 26, [0] * 26
13+
for item in s:
14+
dic1[ord(item) - ord('a')] += 1
15+
for item in t:
16+
dic2[ord(item) - ord('a')] += 1
17+
return dic1 == dic2
18+
19+
20+
def isAnagram3(self, s, t):
21+
return sorted(s) == sorted(t)
22+
23+
24+
if __name__ == '__main__':
25+
s = "anagram"
26+
t = "nagaram"
27+
solution = Solution()
28+
res = solution.isAnagram(s, t)
29+
print(res)

Week 02/id_091/LeetCode_51_091.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
6+
def solveNQueens(self, n: int) -> List[List[str]]:
7+
def DFS(queens, xy_dif, xy_sum):
8+
p = len(queens)
9+
if p == n:
10+
result.append(queens)
11+
return None
12+
for q in range(n):
13+
if q not in queens and p - q not in xy_dif and p + q not in xy_sum:
14+
DFS(queens + [q], xy_dif + [p - q], xy_sum + [p + q])
15+
16+
result = []
17+
DFS([], [], [])
18+
return [["." * i + "Q" + "." * (n - i - 1) for i in sol] for sol in result]
19+
20+
def solveNQueens2(self, n: int) -> List[List[str]]:
21+
res = []
22+
if n == 0:
23+
return res
24+
25+
nums = [i for i in range(n)]
26+
col = set()
27+
master = set()
28+
slave = set()
29+
stack = []
30+
31+
self.__backtracking(nums, 0, n, col, master, slave, stack, res)
32+
return res
33+
34+
def __backtracking(self, nums, row, n, col, master, slave, stack, res):
35+
if row == n:
36+
board = self.__convert2board(stack, n)
37+
res.append(board)
38+
return
39+
40+
for i in range(n):
41+
if i not in col and row + i not in master and row - i not in slave:
42+
stack.append(nums[i])
43+
col.add(i)
44+
master.add(row + i)
45+
slave.add(row - i)
46+
47+
self.__backtracking(nums, row + 1, n, col, master, slave, stack, res)
48+
49+
slave.remove(row - i)
50+
master.remove(row + i)
51+
col.remove(i)
52+
stack.pop()
53+
54+
def __convert2board(self, stack, n):
55+
return ["." * stack[i] + "Q" + "." * (n - stack[i] - 1) for i in range(n)]
56+
57+
58+
if __name__ == '__main__':
59+
n = 5
60+
solution = Solution()
61+
res = solution.solveNQueens(n)
62+
print(res)

Week 02/id_091/LeetCode_94_091.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class Node():
2+
3+
def __init__(self, data=None, left=None, right=None):
4+
self._data = data
5+
self._left = left
6+
self._right = right
7+
8+
9+
def proOrder(tree):
10+
if tree == None:
11+
return False
12+
print(tree._data)
13+
proOrder(tree._left)
14+
proOrder(tree._right)
15+
16+
17+
def posOrder(tree):
18+
if tree == None:
19+
return False
20+
posOrder(tree._left)
21+
posOrder(tree._right)
22+
posOrder(tree._data)
23+
24+
25+
def inOrder(tree):
26+
if tree == None:
27+
return False
28+
inOrder(tree._left)
29+
print(tree._data)
30+
inOrder(tree._right)
31+
32+
33+
# 层次遍历
34+
def rowOrder(tree):
35+
queue = []
36+
queue.append(tree)
37+
while True:
38+
if queue == []:
39+
break
40+
print(queue[0]._data)
41+
first_tree = queue[0]
42+
if first_tree._left != None:
43+
queue.append(first_tree._left)
44+
if first_tree._right != None:
45+
queue.append(first_tree._right)
46+
queue.remove(first_tree)
47+
48+
49+
if __name__ == '__main__':
50+
tree = Node('A', Node('B', Node('D'), Node('E')), Node('C', Node('F'), Node('G')))
51+
proOrder(tree)
52+
# inOrder(tree)
53+
# posOrder(tree)
54+
# rowOrder(tree)

0 commit comments

Comments
 (0)