Skip to content

Commit c9360fa

Browse files
Merge pull request #9 from James-Ren/master
第一周作业#10
2 parents f80e02c + e2739d3 commit c9360fa

12 files changed

+371
-0
lines changed

Week_01/id_10/Leecode-101-10 .py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def isSymmetric(self, root: TreeNode) -> bool:
10+
if not root:
11+
return True
12+
return self._dfs(root.left, root.right)
13+
14+
def _dfs(self, node1, node2):
15+
if not node1 and node2 or not node2 and node1:
16+
return False
17+
if node1 and node2 and node1.val != node2.val:
18+
return False
19+
if not node1 and not node2:
20+
return True
21+
return self._dfs(node1.left, node2.right) and self._dfs(node2.left, node1.right)

Week_01/id_10/Leecode-1021-10.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def removeOuterParentheses(self, S: str) -> str:
3+
result=""
4+
index=0
5+
for c in S:
6+
if c=='(':
7+
if index!=0:
8+
result+=c
9+
index+=1
10+
else:
11+
index-=1
12+
if index!=0:
13+
result+=c
14+
return result

Week_01/id_10/Leecode-174-10.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Solution:
2+
# 第一想法 二分法 没有思路
3+
# 第二想法 dp 从第一个位置递推到最后,没写出来
4+
# 第三想法 暴力法 dfs 遍历所有
5+
# 时间复杂度 O(2^N) 空间复杂度 O(1) N为格子总数
6+
# 提交超时
7+
def calculateMinimumHP(self, dungeon: List[List[int]]) -> int:
8+
if len(dungeon) == 0:
9+
return 0
10+
if len(dungeon[0]) == 0:
11+
return 0
12+
self.dungeon = dungeon
13+
self.minblood = -1
14+
self._dfs(0, 0, 0, dungeon[0][0])
15+
return self.minblood + 1
16+
17+
def _dfs(self, i, j, minblood, routeblood):
18+
m = len(self.dungeon)
19+
n = len(self.dungeon[0])
20+
if minblood < 0 - routeblood:
21+
minblood = 0 - routeblood
22+
if i == m - 1 and j == n - 1:
23+
if self.minblood == -1 or self.minblood > minblood:
24+
self.minblood = minblood
25+
if self.minblood > -1 and self.minblood < minblood:
26+
return
27+
if i < m - 1:
28+
self._dfs(i + 1, j, minblood, routeblood + self.dungeon[i + 1][j])
29+
if j < n - 1:
30+
self._dfs(i, j + 1, minblood, routeblood + self.dungeon[i][j + 1])
31+
32+
# 看了解法和评论区 使用逆序dp
33+
# 从最后一个位置向前推,的确没想到
34+
# 时间复杂度 O(N) 空间复杂度 O(N) N为格子总数
35+
def calculateMinimumHP2(self, dungeon: List[List[int]]) -> int:
36+
if len(dungeon) == 0:
37+
return 0
38+
if len(dungeon[0]) == 0:
39+
return 0
40+
m = len(dungeon)
41+
n = len(dungeon[0])
42+
dp = [[0 for y in range(n)] for x in range(m)]
43+
dp[m - 1][n - 1] = max(1, 1 - dungeon[m - 1][n - 1])
44+
for i in range(m - 2, -1, -1):
45+
dp[i][n - 1] = max(dp[i + 1][n - 1] - dungeon[i][n - 1], 1)
46+
for i in range(n - 2, -1, -1):
47+
dp[m - 1][i] = max(dp[m - 1][i + 1] - dungeon[m - 1][i], 1)
48+
for i in range(m - 2, -1, -1):
49+
for j in range(n - 2, -1, -1):
50+
dp[i][j] = min(max(1, dp[i + 1][j] - dungeon[i][j]), max(1, dp[i][j + 1] - dungeon[i][j]))
51+
return max(dp[0][0], 1)

Week_01/id_10/Leecode-189-10.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class Solution:
2+
def rotate(self, nums: List[int], k: int) -> None:
3+
"""
4+
Do not return anything, modify nums in-place instead.
5+
"""
6+
if len(nums) == 0 or len(nums) == 1:
7+
return
8+
l = len(nums)
9+
if k > l:
10+
k = k % l
11+
12+
endindex = len(nums)
13+
startindex = k
14+
while k > 0:
15+
while startindex + k < endindex:
16+
for i in range(k):
17+
tmp = nums[startindex + i]
18+
nums[startindex + i] = nums[i]
19+
nums[i] = tmp
20+
startindex = startindex + k
21+
if startindex < endindex:
22+
leftednum = endindex - startindex
23+
for i in range(leftednum):
24+
tmp = nums[startindex + i]
25+
nums[startindex + i] = nums[i]
26+
nums[i] = tmp
27+
else:
28+
break
29+
endindex = k
30+
k = k - leftednum
31+
startindex = k
32+
33+
def rotate3(self, nums: List[int], k: int) -> None:
34+
"""
35+
Do not return anything, modify nums in-place instead.
36+
"""
37+
if len(nums) == 0 or len(nums) == 1:
38+
return
39+
l = len(nums)
40+
if k > l:
41+
k = k % l
42+
43+
newarr = nums[len(nums) - k:]
44+
for i in range(len(nums) - k - 1, -1, -1):
45+
nums[i + k] = nums[i]
46+
for i in range(k):
47+
nums[i] = newarr[i]
48+
49+
def rotate2(self, nums: List[int], k: int) -> None:
50+
"""
51+
Do not return anything, modify nums in-place instead.
52+
"""
53+
if len(nums) == 0 or len(nums) == 1:
54+
return
55+
l = len(nums)
56+
if k > l:
57+
k = k % l
58+
59+
for _ in range(k):
60+
lastel = nums[l - 1]
61+
for i in range(l - 2, -1, -1):
62+
nums[i + 1] = nums[i]
63+
nums[0] = lastel

Week_01/id_10/Leecode-21-10.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
9+
cur1,cur2=l1,l2
10+
head,cur=None,None
11+
while cur1 or cur2:
12+
prev=cur
13+
if cur1 and cur2:
14+
if cur1.val>cur2.val:
15+
cur=cur2
16+
cur2=cur2.next
17+
else:
18+
cur=cur1
19+
cur1=cur1.next
20+
elif cur1:
21+
cur=cur1
22+
cur1=cur1.next
23+
else:
24+
cur=cur2
25+
cur2=cur2.next
26+
if not head:
27+
head=cur
28+
if prev:
29+
prev.next=cur
30+
return head

Week_01/id_10/Leecode-236-10.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
10+
if not root or not p and not q:
11+
return None
12+
if root==p or root==q:
13+
return root
14+
leftTree=self.lowestCommonAncestor(root.left,p,q)
15+
rightTree=self.lowestCommonAncestor(root.right,p,q)
16+
if not leftTree and not rightTree:
17+
return None
18+
if leftTree and rightTree:
19+
return root
20+
if leftTree:
21+
return leftTree
22+
return rightTree

Week_01/id_10/Leecode-24-10.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
# 时间复杂度 O(n)
8+
# 空间复杂度 O(1)
9+
class Solution:
10+
def swapPairs(self, head: ListNode) -> ListNode:
11+
if not head or not head.next:
12+
return head
13+
last,node1,node2=None,head,head.next
14+
head=node2
15+
while node2:
16+
nextnode=node2.next
17+
node2.next=node1
18+
if last:
19+
last.next=node2
20+
node1.next=nextnode
21+
if not nextnode:
22+
break
23+
last=node1
24+
node1=nextnode
25+
node2=nextnode.next
26+
return head

Week_01/id_10/Leecode-257-10.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import "strconv"
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* type TreeNode struct {
6+
* Val int
7+
* Left *TreeNode
8+
* Right *TreeNode
9+
* }
10+
*/
11+
var allroutes []string
12+
13+
func binaryTreePaths(root *TreeNode) []string {
14+
if root == nil {
15+
return []string{}
16+
}
17+
allroutes = make([]string, 0)
18+
dfs("", root)
19+
return allroutes
20+
}
21+
22+
func dfs(str string, root *TreeNode) {
23+
curroute := ""
24+
if str == "" {
25+
curroute = strconv.Itoa(root.Val)
26+
} else {
27+
curroute = str + "->" + strconv.Itoa(root.Val)
28+
}
29+
if root.Left == nil && root.Right == nil {
30+
allroutes = append(allroutes, curroute)
31+
return
32+
}
33+
if root.Left != nil {
34+
dfs(curroute, root.Left)
35+
}
36+
if root.Right != nil {
37+
dfs(curroute, root.Right)
38+
}
39+
}

Week_01/id_10/Leecode-42-10.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
func trap(height []int) int {
2+
totals := 0
3+
startpos := 0
4+
for i := 1; i < len(height); i++ {
5+
curh := height[i]
6+
if curh >= height[startpos] {
7+
minh := height[startpos]
8+
curw := i - startpos - 1
9+
if curw == 0 {
10+
startpos = i
11+
continue
12+
}
13+
rangenums := minh * curw
14+
for j := startpos + 1; j < i; j++ {
15+
rangenums -= height[j]
16+
}
17+
totals += rangenums
18+
startpos = i
19+
}
20+
}
21+
biggestpos := startpos
22+
startpos = len(height) - 1
23+
for i := len(height) - 2; i >= biggestpos; i-- {
24+
curh := height[i]
25+
if curh >= height[startpos] {
26+
minh := height[startpos]
27+
curw := startpos - i - 1
28+
if curw == 0 {
29+
startpos = i
30+
continue
31+
}
32+
rangenums := minh * curw
33+
for j := i + 1; j < startpos; j++ {
34+
rangenums -= height[j]
35+
}
36+
totals += rangenums
37+
startpos = i
38+
}
39+
}
40+
return totals
41+
}

Week_01/id_10/Leecode-49-10.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
3+
strmap={}
4+
for s in strs:
5+
ss=''.join(sorted(s))
6+
strarr=strmap.get(ss,[])
7+
strarr.append(s)
8+
strmap[ss]=strarr
9+
return list(strmap.values())

Week_01/id_10/Leecode-50-10.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
# 看了别人的代码仿照写的
3+
# 时间复杂度 O(logN)
4+
# 利用位运算做除2和奇偶判断
5+
# 空间复杂度 O(1)
6+
class Solution:
7+
def myPow(self, x: float, n: int) -> float:
8+
isNegitive=False
9+
if n==0:
10+
return 1.0
11+
if n==1:
12+
return x
13+
if n<0:
14+
isNegitive=True
15+
n=0-n
16+
res,p=1,x
17+
while n!=0:
18+
if n&1==1:
19+
res*=p
20+
p*=p
21+
n>>=1
22+
if isNegitive:
23+
res=1.0/res
24+
return res

Week_01/id_10/Leecode-84-10.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
def largestRectangleArea(self, heights: List[int]) -> int:
3+
if len(heights)==0:
4+
return 0
5+
stack=collections.deque()
6+
maxarea=0
7+
for i in range(len(heights)):
8+
h=heights[i]
9+
l=len(stack)
10+
if l>0:
11+
startindex=stack[len(stack)-1]
12+
while l>0 and heights[startindex]>h:
13+
popindex=stack.pop()
14+
l-=1
15+
startindex=-1
16+
if l>0:
17+
startindex=stack[l-1]
18+
area=(i-startindex-1)*heights[popindex]
19+
if maxarea<area:
20+
maxarea=area
21+
stack.append(i)
22+
lastindex=stack[len(stack)-1]
23+
for i in range(len(stack)-1,-1,-1):
24+
if i>0:
25+
startindex=stack[i-1]
26+
else:
27+
startindex=-1
28+
area=heights[stack[i]]*(lastindex-startindex)
29+
if maxarea<area:
30+
maxarea=area
31+
return maxarea

0 commit comments

Comments
 (0)