Skip to content

Commit 70643a8

Browse files
Merge pull request youngyangyang04#2562 from FEI120483/master
Update Python3
2 parents 145efea + 14809cd commit 70643a8

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

problems/0151.翻转字符串里的单词.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,13 @@ class Solution:
467467
# 将列表转换成字符串
468468
return " ".join(words)
469469
```
470+
(版本三) 拆分字符串 + 反转列表
471+
```python
472+
class Solution:
473+
def reverseWords(self, s):
474+
words = s.split() #type(words) --- list
475+
words = words[::-1] # 反转单词
476+
return ' '.join(words) #列表转换成字符串
470477

471478
### Go:
472479

problems/0455.分发饼干.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,26 @@ class Solution:
206206

207207
```
208208

209-
### Go
209+
栈 大饼干优先
210+
```python
211+
from collecion import deque
212+
class Solution:
213+
def findContentChildren(self, g: List[int], s: List[int]) -> int:
214+
#思路,饼干和孩子按从大到小排序,依次从栈中取出,若满足条件result += 1 否则将饼干栈顶元素重新返回
215+
result = 0
216+
queue_g = deque(sorted(g, reverse = True))
217+
queue_s = deque(sorted(s, reverse = True))
218+
while queue_g and queue_s:
219+
child = queue_g.popleft()
220+
cookies = queue_s.popleft()
221+
if child <= cookies:
222+
result += 1
223+
else:
224+
queue_s.appendleft(cookies)
225+
return result
226+
```
210227

228+
### Go
211229
```golang
212230
//排序后,局部最优
213231
func findContentChildren(g []int, s []int) int {

problems/0700.二叉搜索树中的搜索.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,26 @@ class Solution:
262262
return None
263263
```
264264

265+
(方法三) 栈-遍历
266+
```python
267+
class Solution:
268+
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
269+
stack = [root]
270+
while stack:
271+
node = stack.pop()
272+
# 根据TreeNode的定义
273+
# node携带有三类信息 node.left/node.right/node.val
274+
# 找到val直接返回node 即是找到了该节点为根的子树
275+
# 此处node.left/node.right/val的前后顺序可打乱
276+
if node.val == val:
277+
return node
278+
if node.right:
279+
stack.append(node.right)
280+
if node.left:
281+
stack.append(node.left)
282+
return None
283+
```
284+
265285
266286
### Go
267287

problems/0977.有序数组的平方.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,24 @@ class Solution:
178178
return sorted(x*x for x in nums)
179179
```
180180

181+
```Python
182+
(版本四) 双指针+ 反转列表
183+
class Solution:
184+
def sortedSquares(self, nums: List[int]) -> List[int]:
185+
#根据list的先进排序在先原则
186+
#将nums的平方按从大到小的顺序添加进新的list
187+
#最后反转list
188+
new_list = []
189+
left, right = 0 , len(nums) -1
190+
while left <= right:
191+
if abs(nums[left]) <= abs(nums[right]):
192+
new_list.append(nums[right] ** 2)
193+
right -= 1
194+
else:
195+
new_list.append(nums[left] ** 2)
196+
left += 1
197+
return new_list[::-1]
198+
181199
### Go:
182200

183201
```Go

problems/kamacoder/0055.右旋字符串.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,13 @@ s = s[len(s)-k:] + s[:len(s)-k]
222222
print(s)
223223
```
224224

225+
```Python 切片法
226+
k = int(input())
227+
s = input()
228+
229+
print(s[-k:] + s[:-k])
230+
```
231+
225232
### Go:
226233
```go
227234
package main

0 commit comments

Comments
 (0)