File tree Expand file tree Collapse file tree 5 files changed +71
-1
lines changed Expand file tree Collapse file tree 5 files changed +71
-1
lines changed Original file line number Diff line number Diff line change @@ -467,6 +467,13 @@ class Solution:
467
467
# 将列表转换成字符串
468
468
return " " .join(words)
469
469
```
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) # 列表转换成字符串
470
477
471
478
# ## Go:
472
479
Original file line number Diff line number Diff line change @@ -206,8 +206,26 @@ class Solution:
206
206
207
207
```
208
208
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
+ ```
210
227
228
+ ### Go
211
229
``` golang
212
230
// 排序后,局部最优
213
231
func findContentChildren (g []int , s []int ) int {
Original file line number Diff line number Diff line change @@ -262,6 +262,26 @@ class Solution:
262
262
return None
263
263
```
264
264
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
+
265
285
266
286
### Go
267
287
Original file line number Diff line number Diff line change @@ -178,6 +178,24 @@ class Solution:
178
178
return sorted (x* x for x in nums)
179
179
```
180
180
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
+
181
199
# ## Go:
182
200
183
201
```Go
Original file line number Diff line number Diff line change @@ -222,6 +222,13 @@ s = s[len(s)-k:] + s[:len(s)-k]
222
222
print (s)
223
223
```
224
224
225
+ ``` Python 切片法
226
+ k = int (input ())
227
+ s = input ()
228
+
229
+ print (s[- k:] + s[:- k])
230
+ ```
231
+
225
232
### Go:
226
233
``` go
227
234
package main
You can’t perform that action at this time.
0 commit comments