Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 83e4f99

Browse files
2020年10月15日
1 parent 356ba7d commit 83e4f99

File tree

15 files changed

+402
-216
lines changed

15 files changed

+402
-216
lines changed
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
class Solution(object):
2-
def isValidSudoku(self, board):
3-
"""
4-
:type board: List[List[str]]
5-
:rtype: bool
6-
"""
7-
from collections import defaultdict
8-
row, column, squre = defaultdict(set), defaultdict(set), defaultdict(set)
1+
class Solution:
2+
def isValidSudoku(self, board: List[List[str]]) -> bool:
3+
row = defaultdict(set)
4+
col = defaultdict(set)
5+
square = defaultdict(set)
96
for i in range(9):
107
for j in range(9):
118
if board[i][j].isdigit():
12-
if board[i][j] in row[i] or board[i][j] in column[j] or board[i][j] in squre[(i//3, j//3)]:
9+
if board[i][j] in row[i] or board[i][j] in col[j] or board[i][j] in square[(i//3, j//3)]:
1310
return False
1411
else:
1512
row[i].add(board[i][j])
16-
column[j].add(board[i][j])
17-
squre[(i//3, j //3)].add(board[i][j])
13+
col[j].add(board[i][j])
14+
square[(i//3, j //3)].add(board[i][j])
1815
return True
19-

‎0037.解数独/0037-解数独.py‎

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,66 @@
1-
class Solution(object):
2-
def solveSudoku(self, board):
1+
from collections import defaultdict
2+
import copy
3+
class Solution:
4+
def solveSudoku(self, board: List[List[str]]) -> None:
35
"""
4-
:type board: List[List[str]]
5-
:rtype: None Do not return anything, modify board in-place instead.
6+
Do not return anything, modify board in-place instead.
67
"""
7-
from collections import defaultdict
8-
row, column, squre = defaultdict(set), defaultdict(set), defaultdict(set)
9-
8+
row = defaultdict(set)
9+
col = defaultdict(set)
10+
square = defaultdict(set)
11+
for i in range(9):
12+
for j in range(9):
13+
if board[i][j].isdigit():
14+
row[i].add(board[i][j])
15+
col[j].add(board[i][j])
16+
square[(i // 3, j // 3)].add(board[i][j])
1017
self.res = []
1118
def dfs(x, y):
12-
13-
if x == 8 and y == 9:
14-
# print board
15-
for roww in board:
16-
self.res.append(roww[:])
17-
# print self.res
19+
if x == 9 and y == 0:
20+
if self.isValidSudoku(board):
21+
self.res = copy.deepcopy(board)
1822
return
19-
if y == 9:
20-
dfs(x + 1, 0)
21-
return
22-
if board[x][y].isdigit():
23-
dfs(x, y + 1)
24-
return
25-
26-
for k in range(1,10):
27-
if str(k) not in row[x] and str(k) not in column[y] and str(k) not in squre[(x // 3, y // 3)]:
28-
board[x][y] = str(k)
29-
row[x].add(str(k))
30-
column[y].add(str(k))
31-
squre[(x // 3, y // 3)].add(str(k))
32-
33-
dfs(x, y + 1)
34-
35-
board[x][y] = "."
36-
row[x].remove(str(k))
37-
column[y].remove(str(k))
38-
squre[(x // 3, y // 3)].remove(str(k))
3923

24+
if not self.res:
25+
if board[x][y] != ".":
26+
if y == 8:
27+
dfs(x + 1, 0)
28+
else:
29+
dfs(x, y + 1)
30+
return
31+
32+
for num in range(1, 10):
33+
num = str(num)
34+
if num not in row[x] and num not in col[y] and num not in square[(x // 3, y // 3)]:
35+
board[x][y] = num
36+
37+
row[x].add(num)
38+
col[y].add(num)
39+
square[(x // 3, y // 3)].add(num)
40+
if y == 8:
41+
dfs(x + 1, 0)
42+
else:
43+
dfs(x, y + 1)
44+
board[x][y] = "."
45+
row[x].remove(num)
46+
col[y].remove(num)
47+
square[(x // 3, y // 3)].remove(num)
48+
49+
dfs(0, 0)
50+
board[:] = self.res
51+
52+
53+
def isValidSudoku(self, board: List[List[str]]) -> bool:
54+
row = defaultdict(set)
55+
col = defaultdict(set)
56+
square = defaultdict(set)
4057
for i in range(9):
4158
for j in range(9):
4259
if board[i][j].isdigit():
43-
row[i].add(board[i][j].encode("utf-8"))
44-
column[j].add(board[i][j].encode("utf-8"))
45-
squre[(i // 3, j // 3)].add(board[i][j].encode("utf-8"))
46-
47-
dfs(0, 0)
48-
board[:] = self.res
60+
if board[i][j] in row[i] or board[i][j] in col[j] or board[i][j] in square[(i // 3, j // 3)]:
61+
return False
62+
else:
63+
row[i].add(board[i][j])
64+
col[j].add(board[i][j])
65+
square[(i // 3, j // 3)].add(board[i][j])
66+
return True
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
class Solution(object):
2-
def largestRectangleArea(self, heights):
3-
"""
4-
:type heights: List[int]
5-
:rtype: int
6-
"""
1+
class Solution:
2+
def largestRectangleArea(self, heights: List[int]) -> int:
73
heights = [0] + heights + [0]
8-
stack = []
94
res = 0
10-
for i, num in enumerate(heights):
11-
while stack and heights[stack[-1]] > num:
12-
top = stack.pop()
13-
res = max(res, (i - stack[-1] - 1) * heights[top])
5+
stack = []
6+
for i in range(len(heights)):
7+
while stack and heights[stack[-1]] > heights[i]:
8+
top = stack.pop()
9+
res = max(res, (i - stack[-1] - 1) * heights[top])
10+
1411
stack.append(i)
1512
return res
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution:
2+
def numDecodings(self, s: str) -> int:
3+
if not s or s[0] == "0":
4+
return 0
5+
6+
dp = [0]*(len(s) + 1) # dp[i] represents ways of s[:i + 1]
7+
dp[1] = dp[0] = 1
8+
9+
for i in range(2, len(s) + 1):
10+
if s[i - 1] == "0":
11+
if s[i - 2] in ["1", "2"]:
12+
dp[i] = dp[i - 2]
13+
else:
14+
return 0
15+
elif s[i - 2] == "1" or (s[i - 2] == "2" and "1" <= s[i - 1] <= "6"):
16+
dp[i] = dp[i - 1] + dp[i - 2]
17+
else:
18+
dp[i] = dp[i - 1]
19+
return dp[-1]
20+
21+
# dp = [0]*(len(s) + 1)
22+
# if s[0] == '0':
23+
# return 0
24+
# dp[0] = 1
25+
# dp[1] = 1
26+
# for i in range(2, len(s)+1):
27+
# if s[i-1] == '0' :
28+
# if s[i-2] in ['1', '2']:
29+
# dp[i] = dp[i-2]
30+
# else:
31+
# return 0
32+
# elif s[i-2] == '1' or (s[i-2] == '2' and '1' <= s[i-1] <= '6'):
33+
# dp[i] = dp[i-1] + dp[i-2]
34+
# else:
35+
# dp[i] = dp[i-1]
36+
# return dp[-1]
37+
38+
39+
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
# Definition for a binary tree node.
2-
# class TreeNode(object):
2+
# class TreeNode:
33
# def __init__(self, val=0, left=None, right=None):
44
# self.val = val
55
# self.left = left
66
# self.right = right
7-
class Solution(object):
8-
def isSameTree(self, p, q):
9-
"""
10-
:type p: TreeNode
11-
:type q: TreeNode
12-
:rtype: bool
13-
"""
7+
class Solution:
8+
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
149
if not p and not q:
1510
return True
11+
1612
if not p and q:
1713
return False
14+
1815
if p and not q:
19-
return False
16+
return False
17+
2018
return p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
class Solution(object):
2-
def wordBreak(self, s, wordDict):
3-
"""
4-
:type s: str
5-
:type wordDict: List[str]
6-
:rtype: bool
7-
"""
8-
dp = [0]
9-
10-
for j in range(len(s) + 1):
11-
for i in dp:
12-
if s[i:j] in wordDict:
13-
dp.append(j)
1+
class Solution:
2+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
3+
from collections import deque
4+
wordDict = set(wordDict)
5+
record = [0]
6+
7+
for i in range(len(s) + 1):
8+
for j in record:
9+
if s[j:i] in wordDict:
10+
record.append(i)
1411
break
15-
# print dp
16-
return dp[-1] == len(s)
17-
12+
# print (record)
13+
return record[-1] == len(s)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
3+
4+
def helper(s, memo):
5+
if s in memo:
6+
return memo[s]
7+
if not s:
8+
return []
9+
res = []
10+
for word in wordDict:
11+
if not s.startswith(word):
12+
continue
13+
if len(word) == len(s):
14+
res.append(word)
15+
else:
16+
resultOfTheRest = helper(s[len(word):], memo)
17+
for item in resultOfTheRest:
18+
item = word + ' ' + item
19+
res.append(item)
20+
memo[s] = res
21+
return res
22+
return helper(s, {})

‎0207.课程表/0207-课程表.py‎

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
1-
class Solution(object):
2-
def canFinish(self, numCourses, prerequisites):
3-
"""
4-
:type numCourses: int
5-
:type prerequisites: List[List[int]]
6-
:rtype: bool
7-
"""
8-
from collections import deque
9-
if not prerequisites: #没有前置课的要求
10-
return True
11-
12-
indegree = [0 for _ in range(numCourses)]
13-
adj = [set() for _ in range(numCourses)]
14-
15-
for end, start in prerequisites:
16-
indegree[end] += 1
17-
adj[start].add(end)
18-
19-
queue = deque()
20-
for i, x in enumerate(indegree):
21-
if not x: #入度为0的结点入队
22-
queue.append(i)
23-
24-
cnt = 0
1+
class Solution:
2+
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
3+
# 1. find all node/course with indegree 0, let them enter a queue
4+
from collections import defaultdict, deque
5+
indegree = defaultdict(int)
6+
children = defaultdict(set)
7+
all_courses = set()
8+
for cur, pre in prerequisites:
9+
indegree[cur] += 1
10+
children[pre].add(cur)
11+
all_courses.add(cur)
12+
all_courses.add(pre)
13+
14+
queue = deque([])
15+
for course in all_courses:
16+
if indegree[course] == 0:
17+
queue.append(course)
18+
# 2. BFS, let course with indegree 0 leave a queue, and let its children with indegree 0 into the queue
19+
studied_course = 0
2520
while queue:
2621
cur = queue.popleft()
27-
cnt += 1 #当前的cur满足条件
28-
29-
for neighbor in adj[cur]:
30-
indegree[neighbor] -= 1
31-
if not indegree[neighbor]:
32-
queue.append(neighbor)
33-
34-
return cnt == numCourses
35-
22+
23+
studied_course += 1
24+
for child in children[cur]:
25+
indegree[child] -= 1
26+
if indegree[child] == 0:
27+
queue.append(child)
28+
29+
return studied_course == len(all_courses)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /