From bf863d2d8c0e5316ba1d41e1a22f76f883ff8bc1 Mon Sep 17 00:00:00 2001 From: heroding77 <2441145504@qq.com> Date: Mon, 26 Feb 2024 16:04:47 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A00027=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E5=85=83=E7=B4=A0=20python3=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index dbde3d19..2a2005d7 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -253,6 +253,27 @@ class Solution: ``` +``` python 3 +# 相向双指针法 +# 时间复杂度 O(n) +# 空间复杂度 O(1) +class Solution: + def removeElement(self, nums: List[int], val: int) -> int: + n = len(nums) + left, right = 0, n - 1 + while left <= right: + while left <= right and nums[left] != val: + left += 1 + while left <= right and nums[right] == val: + right -= 1 + if left < right: + nums[left] = nums[right] + left += 1 + right -= 1 + return left + +``` + ### Go: ```go From 190dd0e54a45208983d3e7c8a3437221b01f0270 Mon Sep 17 00:00:00 2001 From: Jamie He Date: Wed, 28 Feb 2024 06:06:07 +0000 Subject: [PATCH 2/5] =?UTF-8?q?used=20=E6=95=B0=E7=BB=84=E8=B6=85=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=20=E7=A7=BB=E9=99=A4=E7=9B=B8=E5=85=B3=E7=AD=94?= =?UTF-8?q?=E6=A1=88=E3=80=820332.=E9=87=8D=E6=96=B0=E5=AE=89=E6=8E=92?= =?UTF-8?q?=E8=A1=8C=E7=A8=8B.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0332.重新安排行程.md | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/problems/0332.重新安排行程.md b/problems/0332.重新安排行程.md index 036cfc51..61fa82f5 100644 --- a/problems/0332.重新安排行程.md +++ b/problems/0332.重新安排行程.md @@ -419,32 +419,6 @@ class Solution { ``` ### Python -回溯 使用used数组 - -```python -class Solution: - def findItinerary(self, tickets: List[List[str]]) -> List[str]: - tickets.sort() # 先排序,这样一旦找到第一个可行路径,一定是字母排序最小的 - used = [0] * len(tickets) - path = ['JFK'] - results = [] - self.backtracking(tickets, used, path, 'JFK', results) - return results[0] - - def backtracking(self, tickets, used, path, cur, results): - if len(path) == len(tickets) + 1: # 终止条件:路径长度等于机票数量+1 - results.append(path[:]) # 将当前路径添加到结果列表 - return True - - for i, ticket in enumerate(tickets): # 遍历机票列表 - if ticket[0] == cur and used[i] == 0: # 找到起始机场为cur且未使用过的机票 - used[i] = 1 # 标记该机票为已使用 - path.append(ticket[1]) # 将到达机场添加到路径中 - state = self.backtracking(tickets, used, path, ticket[1], results) # 递归搜索 - path.pop() # 回溯,移除最后添加的到达机场 - used[i] = 0 # 标记该机票为未使用 - if state: - return True # 只要找到一个可行路径就返回,不继续搜索 ``` 回溯 使用字典 From da5be794f9f7923197465570c56870a4dda735b7 Mon Sep 17 00:00:00 2001 From: Jamie He Date: Wed, 28 Feb 2024 06:10:39 +0000 Subject: [PATCH 3/5] =?UTF-8?q?Update=200332.=E9=87=8D=E6=96=B0=E5=AE=89?= =?UTF-8?q?=E6=8E=92=E8=A1=8C=E7=A8=8B.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0332.重新安排行程.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/problems/0332.重新安排行程.md b/problems/0332.重新安排行程.md index 61fa82f5..6c8a4814 100644 --- a/problems/0332.重新安排行程.md +++ b/problems/0332.重新安排行程.md @@ -418,8 +418,7 @@ class Solution { } ``` -### Python - +### Python ``` 回溯 使用字典 ```python From f926ac8f390f63236426ca29f2bfd63ad98685c9 Mon Sep 17 00:00:00 2001 From: Tiana <51739436+tianci-zhang@users.noreply.github.com> Date: Thu, 29 Feb 2024 18:45:42 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A00070.=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF=E5=AE=8C=E5=85=A8=E8=83=8C=E5=8C=85=E7=89=88=E6=9C=AC?= =?UTF-8?q?.md=20Python3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加0070.爬楼梯完全背包版本.md Python3代码 --- problems/0070.爬楼梯完全背包版本.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index 4fa294cf..622b1117 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -165,7 +165,21 @@ class climbStairs{ ``` ### Python3: +```python3 +def climbing_stairs(n,m): + dp = [0]*(n+1) # 背包总容量 + dp[0] = 1 + # 排列题,注意循环顺序,背包在外物品在内 + for j in range(1,n+1): + for i in range(1,m+1): + if j>=i: + dp[j] += dp[j-i] # 这里i就是重量而非index + return dp[n] +if __name__ == '__main__': + n,m = list(map(int,input().split(' '))) + print(climbing_stairs(n,m)) +``` ### Go: From a1e8af39a8ab834c08ba163be920c29add7d513f Mon Sep 17 00:00:00 2001 From: lavaicer <52038323+lavaicer@users.noreply.github.com> Date: Sat, 2 Mar 2024 01:31:30 +0000 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=2019.=20=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=AC?= =?UTF-8?q?=20N=20=E4=B8=AA=E7=BB=93=E7=82=B9=20go=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0019.删除链表的倒数第N个节点.md | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index f508b523..d04df9f3 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -165,20 +165,17 @@ class Solution: * } */ func removeNthFromEnd(head *ListNode, n int) *ListNode { - dummyHead := &ListNode{} - dummyHead.Next = head - cur := head - prev := dummyHead - i := 1 - for cur != nil { - cur = cur.Next - if i > n { - prev = prev.Next - } - i++ - } - prev.Next = prev.Next.Next - return dummyHead.Next + dummyNode := &ListNode{0, head} + fast, slow := dummyNode, dummyNode + for i := 0; i <= n; i++ { // 注意<=,否则快指针为空时,慢指针正好在倒数第n个上面 + fast = fast.Next + } + for fast != nil { + fast = fast.Next + slow = slow.Next + } + slow.Next = slow.Next.Next + return dummyNode.Next } ```