Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Week_01/id_10/Leecode-101-10 .py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
if not root:
return True
return self._dfs(root.left, root.right)

def _dfs(self, node1, node2):
if not node1 and node2 or not node2 and node1:
return False
if node1 and node2 and node1.val != node2.val:
return False
if not node1 and not node2:
return True
return self._dfs(node1.left, node2.right) and self._dfs(node2.left, node1.right)
14 changes: 14 additions & 0 deletions Week_01/id_10/Leecode-1021-10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def removeOuterParentheses(self, S: str) -> str:
result=""
index=0
for c in S:
if c=='(':
if index!=0:
result+=c
index+=1
else:
index-=1
if index!=0:
result+=c
return result
51 changes: 51 additions & 0 deletions Week_01/id_10/Leecode-174-10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Solution:
# 第一想法 二分法 没有思路
# 第二想法 dp 从第一个位置递推到最后,没写出来
# 第三想法 暴力法 dfs 遍历所有
# 时间复杂度 O(2^N) 空间复杂度 O(1) N为格子总数
# 提交超时
def calculateMinimumHP(self, dungeon: List[List[int]]) -> int:
if len(dungeon) == 0:
return 0
if len(dungeon[0]) == 0:
return 0
self.dungeon = dungeon
self.minblood = -1
self._dfs(0, 0, 0, dungeon[0][0])
return self.minblood + 1

def _dfs(self, i, j, minblood, routeblood):
m = len(self.dungeon)
n = len(self.dungeon[0])
if minblood < 0 - routeblood:
minblood = 0 - routeblood
if i == m - 1 and j == n - 1:
if self.minblood == -1 or self.minblood > minblood:
self.minblood = minblood
if self.minblood > -1 and self.minblood < minblood:
return
if i < m - 1:
self._dfs(i + 1, j, minblood, routeblood + self.dungeon[i + 1][j])
if j < n - 1:
self._dfs(i, j + 1, minblood, routeblood + self.dungeon[i][j + 1])

# 看了解法和评论区 使用逆序dp
# 从最后一个位置向前推,的确没想到
# 时间复杂度 O(N) 空间复杂度 O(N) N为格子总数
def calculateMinimumHP2(self, dungeon: List[List[int]]) -> int:
if len(dungeon) == 0:
return 0
if len(dungeon[0]) == 0:
return 0
m = len(dungeon)
n = len(dungeon[0])
dp = [[0 for y in range(n)] for x in range(m)]
dp[m - 1][n - 1] = max(1, 1 - dungeon[m - 1][n - 1])
for i in range(m - 2, -1, -1):
dp[i][n - 1] = max(dp[i + 1][n - 1] - dungeon[i][n - 1], 1)
for i in range(n - 2, -1, -1):
dp[m - 1][i] = max(dp[m - 1][i + 1] - dungeon[m - 1][i], 1)
for i in range(m - 2, -1, -1):
for j in range(n - 2, -1, -1):
dp[i][j] = min(max(1, dp[i + 1][j] - dungeon[i][j]), max(1, dp[i][j + 1] - dungeon[i][j]))
return max(dp[0][0], 1)
63 changes: 63 additions & 0 deletions Week_01/id_10/Leecode-189-10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if len(nums) == 0 or len(nums) == 1:
return
l = len(nums)
if k > l:
k = k % l

endindex = len(nums)
startindex = k
while k > 0:
while startindex + k < endindex:
for i in range(k):
tmp = nums[startindex + i]
nums[startindex + i] = nums[i]
nums[i] = tmp
startindex = startindex + k
if startindex < endindex:
leftednum = endindex - startindex
for i in range(leftednum):
tmp = nums[startindex + i]
nums[startindex + i] = nums[i]
nums[i] = tmp
else:
break
endindex = k
k = k - leftednum
startindex = k

def rotate3(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if len(nums) == 0 or len(nums) == 1:
return
l = len(nums)
if k > l:
k = k % l

newarr = nums[len(nums) - k:]
for i in range(len(nums) - k - 1, -1, -1):
nums[i + k] = nums[i]
for i in range(k):
nums[i] = newarr[i]

def rotate2(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if len(nums) == 0 or len(nums) == 1:
return
l = len(nums)
if k > l:
k = k % l

for _ in range(k):
lastel = nums[l - 1]
for i in range(l - 2, -1, -1):
nums[i + 1] = nums[i]
nums[0] = lastel
30 changes: 30 additions & 0 deletions Week_01/id_10/Leecode-21-10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
cur1,cur2=l1,l2
head,cur=None,None
while cur1 or cur2:
prev=cur
if cur1 and cur2:
if cur1.val>cur2.val:
cur=cur2
cur2=cur2.next
else:
cur=cur1
cur1=cur1.next
elif cur1:
cur=cur1
cur1=cur1.next
else:
cur=cur2
cur2=cur2.next
if not head:
head=cur
if prev:
prev.next=cur
return head
22 changes: 22 additions & 0 deletions Week_01/id_10/Leecode-236-10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if not root or not p and not q:
return None
if root==p or root==q:
return root
leftTree=self.lowestCommonAncestor(root.left,p,q)
rightTree=self.lowestCommonAncestor(root.right,p,q)
if not leftTree and not rightTree:
return None
if leftTree and rightTree:
return root
if leftTree:
return leftTree
return rightTree
26 changes: 26 additions & 0 deletions Week_01/id_10/Leecode-24-10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

# 时间复杂度 O(n)
# 空间复杂度 O(1)
class Solution:
def swapPairs(self, head: ListNode) -> ListNode:
if not head or not head.next:
return head
last,node1,node2=None,head,head.next
head=node2
while node2:
nextnode=node2.next
node2.next=node1
if last:
last.next=node2
node1.next=nextnode
if not nextnode:
break
last=node1
node1=nextnode
node2=nextnode.next
return head
39 changes: 39 additions & 0 deletions Week_01/id_10/Leecode-257-10.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import "strconv"

/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
var allroutes []string

func binaryTreePaths(root *TreeNode) []string {
if root == nil {
return []string{}
}
allroutes = make([]string, 0)
dfs("", root)
return allroutes
}

func dfs(str string, root *TreeNode) {
curroute := ""
if str == "" {
curroute = strconv.Itoa(root.Val)
} else {
curroute = str + "->" + strconv.Itoa(root.Val)
}
if root.Left == nil && root.Right == nil {
allroutes = append(allroutes, curroute)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

這題我沒用全局變量,然後 append 的時候踩坑。

原來要覆蓋掉原本的 slice header。

if root.Left == nil && root.Right == nil {
  *res = append(*res, path)
}

https://github.com/algorithm002/algorithm/pull/16/files#diff-a90a3d84391f403040404174d3733521R34

return
}
if root.Left != nil {
dfs(curroute, root.Left)
}
if root.Right != nil {
dfs(curroute, root.Right)
}
}
41 changes: 41 additions & 0 deletions Week_01/id_10/Leecode-42-10.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
func trap(height []int) int {
totals := 0
startpos := 0
for i := 1; i < len(height); i++ {
curh := height[i]
if curh >= height[startpos] {
minh := height[startpos]
curw := i - startpos - 1
if curw == 0 {
startpos = i
continue
}
rangenums := minh * curw
for j := startpos + 1; j < i; j++ {
rangenums -= height[j]
}
totals += rangenums
startpos = i
}
}
biggestpos := startpos
startpos = len(height) - 1
for i := len(height) - 2; i >= biggestpos; i-- {
curh := height[i]
if curh >= height[startpos] {
minh := height[startpos]
curw := startpos - i - 1
if curw == 0 {
startpos = i
continue
}
rangenums := minh * curw
for j := i + 1; j < startpos; j++ {
rangenums -= height[j]
}
totals += rangenums
startpos = i
}
}
return totals
}
9 changes: 9 additions & 0 deletions Week_01/id_10/Leecode-49-10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
strmap={}
for s in strs:
ss=''.join(sorted(s))
strarr=strmap.get(ss,[])
strarr.append(s)
strmap[ss]=strarr
return list(strmap.values())
24 changes: 24 additions & 0 deletions Week_01/id_10/Leecode-50-10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

# 看了别人的代码仿照写的
# 时间复杂度 O(logN)
# 利用位运算做除2和奇偶判断
# 空间复杂度 O(1)
class Solution:
def myPow(self, x: float, n: int) -> float:
isNegitive=False
if n==0:
return 1.0
if n==1:
return x
if n<0:
isNegitive=True
n=0-n
res,p=1,x
while n!=0:
if n&1==1:
res*=p
p*=p
n>>=1
if isNegitive:
res=1.0/res
return res
31 changes: 31 additions & 0 deletions Week_01/id_10/Leecode-84-10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
if len(heights)==0:
return 0
stack=collections.deque()
maxarea=0
for i in range(len(heights)):
h=heights[i]
l=len(stack)
if l>0:
startindex=stack[len(stack)-1]
while l>0 and heights[startindex]>h:
popindex=stack.pop()
l-=1
startindex=-1
if l>0:
startindex=stack[l-1]
area=(i-startindex-1)*heights[popindex]
if maxarea<area:
maxarea=area
stack.append(i)
lastindex=stack[len(stack)-1]
for i in range(len(stack)-1,-1,-1):
if i>0:
startindex=stack[i-1]
else:
startindex=-1
area=heights[stack[i]]*(lastindex-startindex)
if maxarea<area:
maxarea=area
return maxarea