From 8452d5f06743e037eac7dab0898e7ed4e5a2f2af Mon Sep 17 00:00:00 2001 From: Henry Zheng <1204831218@qq.com> Date: Tue, 26 Mar 2024 11:23:25 +0800 Subject: [PATCH 1/4] add a new python version --- problems/0977.有序数组的平方.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index 5bdbcbc7..bb311ca2 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -178,6 +178,24 @@ class Solution: return sorted(x*x for x in nums) ``` +```Python +(版本四) 双指针+ 反转列表 +class Solution: + def sortedSquares(self, nums: List[int]) -> List[int]: + #根据list的先进排序在先原则 + #将nums的平方按从大到小的顺序添加进新的list + #最后反转list + new_list = [] + left, right = 0 , len(nums) -1 + while left <= right: + if abs(nums[left]) <= abs(nums[right]): + new_list.append(nums[right] ** 2) + right -= 1 + else: + new_list.append(nums[left] ** 2) + left += 1 + return new_list[::-1] + ### Go: ```Go From 20db66e8fb5dbc39d7772184e19a98eb412f930e Mon Sep 17 00:00:00 2001 From: Henry Zheng <1204831218@qq.com> Date: Fri, 29 Mar 2024 11:11:39 +0800 Subject: [PATCH 2/4] =?UTF-8?q?update=200151.=E7=BF=BB=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D=20python?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0151.翻转字符串里的单词.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index 520f17a7..7a37ad07 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -467,6 +467,13 @@ class Solution: # 将列表转换成字符串 return " ".join(words) ``` +(版本三) 拆分字符串 + 反转列表 +```python +class Solution: + def reverseWords(self, s): + words = s.split() #type(words) --- list + words = words[::-1] # 反转单词 + return ' '.join(words) #列表转换成字符串 ### Go: From e5c4d56d01713495d0287b7a0a381e326f1b34f7 Mon Sep 17 00:00:00 2001 From: Henry Zheng <1204831218@qq.com> Date: Fri, 29 Mar 2024 11:26:09 +0800 Subject: [PATCH 3/4] =?UTF-8?q?update=20kama55.=E5=8F=B3=E6=97=8B=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2.md=20python=20=E5=88=87=E7=89=87=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/kama55.右旋字符串.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/problems/kama55.右旋字符串.md b/problems/kama55.右旋字符串.md index 71371860..0188918e 100644 --- a/problems/kama55.右旋字符串.md +++ b/problems/kama55.右旋字符串.md @@ -222,6 +222,13 @@ s = s[len(s)-k:] + s[:len(s)-k] print(s) ``` +```Python 切片法 +k = int(input()) +s = input() + +print(s[-k:] + s[:-k]) +``` + ### Go: ```go package main From 784ed2a68c2eb3a790bda49dc5fbbee797f99fbe Mon Sep 17 00:00:00 2001 From: Henry Zheng <1204831218@qq.com> Date: Mon, 20 May 2024 08:51:59 +0800 Subject: [PATCH 4/4] =?UTF-8?q?Update=20#0455=20=E5=88=86=E5=8F=91?= =?UTF-8?q?=E9=A5=BC=E5=B9=B2=20=E6=A0=88=20=E5=A4=A7=E9=A5=BC=E5=B9=B2?= =?UTF-8?q?=E4=BC=98=E5=85=88=20&=20#700=20=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=90=9C=E7=B4=A2=20?= =?UTF-8?q?=E6=A0=88-=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0455.分发饼干.md | 20 +++++++++++++++++++- problems/0700.二叉搜索树中的搜索.md | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 6ae206db..b6f5bae5 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -206,8 +206,26 @@ class Solution: ``` -### Go +栈 大饼干优先 +```python +from collecion import deque +class Solution: + def findContentChildren(self, g: List[int], s: List[int]) -> int: + #思路,饼干和孩子按从大到小排序,依次从栈中取出,若满足条件result += 1 否则将饼干栈顶元素重新返回 + result = 0 + queue_g = deque(sorted(g, reverse = True)) + queue_s = deque(sorted(s, reverse = True)) + while queue_g and queue_s: + child = queue_g.popleft() + cookies = queue_s.popleft() + if child <= cookies: + result += 1 + else: + queue_s.appendleft(cookies) + return result +``` +### Go ```golang //排序后,局部最优 func findContentChildren(g []int, s []int) int { diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md index 9efb1e05..58ada3cb 100644 --- a/problems/0700.二叉搜索树中的搜索.md +++ b/problems/0700.二叉搜索树中的搜索.md @@ -262,6 +262,26 @@ class Solution: return None ``` +(方法三) 栈-遍历 +```python +class Solution: + def searchBST(self, root: TreeNode, val: int) -> TreeNode: + stack = [root] + while stack: + node = stack.pop() + # 根据TreeNode的定义 + # node携带有三类信息 node.left/node.right/node.val + # 找到val直接返回node 即是找到了该节点为根的子树 + # 此处node.left/node.right/val的前后顺序可打乱 + if node.val == val: + return node + if node.right: + stack.append(node.right) + if node.left: + stack.append(node.left) + return None +``` + ### Go