mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
@ -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 {
|
||||
|
@ -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
|
||||
|
||||
|
Reference in New Issue
Block a user