diff --git a/Week_01/id_10/Leecode-101-10 .py b/Week_01/id_10/Leecode-101-10 .py new file mode 100644 index 00000000..50def597 --- /dev/null +++ b/Week_01/id_10/Leecode-101-10 .py @@ -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) diff --git a/Week_01/id_10/Leecode-1021-10.py b/Week_01/id_10/Leecode-1021-10.py new file mode 100644 index 00000000..a98c1f9b --- /dev/null +++ b/Week_01/id_10/Leecode-1021-10.py @@ -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 \ No newline at end of file diff --git a/Week_01/id_10/Leecode-174-10.py b/Week_01/id_10/Leecode-174-10.py new file mode 100644 index 00000000..681c1edf --- /dev/null +++ b/Week_01/id_10/Leecode-174-10.py @@ -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) \ No newline at end of file diff --git a/Week_01/id_10/Leecode-189-10.py b/Week_01/id_10/Leecode-189-10.py new file mode 100644 index 00000000..18970d48 --- /dev/null +++ b/Week_01/id_10/Leecode-189-10.py @@ -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 \ No newline at end of file diff --git a/Week_01/id_10/Leecode-21-10.py b/Week_01/id_10/Leecode-21-10.py new file mode 100644 index 00000000..3be56211 --- /dev/null +++ b/Week_01/id_10/Leecode-21-10.py @@ -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 \ No newline at end of file diff --git a/Week_01/id_10/Leecode-236-10.py b/Week_01/id_10/Leecode-236-10.py new file mode 100644 index 00000000..85d72421 --- /dev/null +++ b/Week_01/id_10/Leecode-236-10.py @@ -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 \ No newline at end of file diff --git a/Week_01/id_10/Leecode-24-10.py b/Week_01/id_10/Leecode-24-10.py new file mode 100644 index 00000000..d1726ff0 --- /dev/null +++ b/Week_01/id_10/Leecode-24-10.py @@ -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 \ No newline at end of file diff --git a/Week_01/id_10/Leecode-257-10.go b/Week_01/id_10/Leecode-257-10.go new file mode 100644 index 00000000..131e299b --- /dev/null +++ b/Week_01/id_10/Leecode-257-10.go @@ -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) + return + } + if root.Left != nil { + dfs(curroute, root.Left) + } + if root.Right != nil { + dfs(curroute, root.Right) + } +} diff --git a/Week_01/id_10/Leecode-42-10.go b/Week_01/id_10/Leecode-42-10.go new file mode 100644 index 00000000..9da96323 --- /dev/null +++ b/Week_01/id_10/Leecode-42-10.go @@ -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 +} diff --git a/Week_01/id_10/Leecode-49-10.py b/Week_01/id_10/Leecode-49-10.py new file mode 100644 index 00000000..9226ea0a --- /dev/null +++ b/Week_01/id_10/Leecode-49-10.py @@ -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()) \ No newline at end of file diff --git a/Week_01/id_10/Leecode-50-10.py b/Week_01/id_10/Leecode-50-10.py new file mode 100644 index 00000000..9a3997ba --- /dev/null +++ b/Week_01/id_10/Leecode-50-10.py @@ -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 \ No newline at end of file diff --git a/Week_01/id_10/Leecode-84-10.py b/Week_01/id_10/Leecode-84-10.py new file mode 100644 index 00000000..f2ac3194 --- /dev/null +++ b/Week_01/id_10/Leecode-84-10.py @@ -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 maxarea0: + startindex=stack[i-1] + else: + startindex=-1 + area=heights[stack[i]]*(lastindex-startindex) + if maxarea