Merge pull request #1164 from SianXiaoCHN/master

python3_0941.有效的山脉数组更新双指针方法_0225.用队列实现栈添加单队列优化方法_0112.路径总和精简递归回溯和递归返回条件,添加迭代法_0860.柠檬水找零 去除无用变量
This commit is contained in:
程序员Carl
2022-04-04 14:31:54 +08:00
committed by GitHub
5 changed files with 67 additions and 31 deletions

View File

@ -496,25 +496,20 @@ class solution:
def pathsum(self, root: treenode, targetsum: int) -> list[list[int]]:
def traversal(cur_node, remain):
if not cur_node.left and not cur_node.right and remain == 0:
result.append(path[:])
return
if not cur_node.left and not cur_node.right: return
if not cur_node.left and not cur_node.right:
if remain == 0:
result.append(path[:])
return
if cur_node.left:
path.append(cur_node.left.val)
remain -= cur_node.left.val
traversal(cur_node.left, remain)
traversal(cur_node.left, remain-cur_node.left.val)
path.pop()
remain += cur_node.left.val
if cur_node.right:
path.append(cur_node.right.val)
remain -= cur_node.right.val
traversal(cur_node.right, remain)
traversal(cur_node.right, remain-cur_node.left.val)
path.pop()
remain += cur_node.right.val
result, path = [], []
if not root:
@ -524,6 +519,30 @@ class solution:
return result
```
**迭代法,用第二个队列保存目前的总和与路径**
```python
class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
if not root:
return []
que, temp = deque([root]), deque([(root.val, [root.val])])
result = []
while que:
for _ in range(len(que)):
node = que.popleft()
value, path = temp.popleft()
if (not node.left) and (not node.right):
if value == targetSum:
result.append(path)
if node.left:
que.append(node.left)
temp.append((node.left.val+value, path+[node.left.val]))
if node.right:
que.append(node.right)
temp.append((node.right.val+value, path+[node.right.val]))
return result
```
## go
112. 路径总和

View File

@ -354,6 +354,32 @@ class MyStack:
return len(self.queue_in) == 0
```
优化,使用一个队列实现
```python
class MyStack:
def __init__(self):
self.que = deque()
def push(self, x: int) -> None:
self.que.append(x)
def pop(self) -> int:
if self.empty():
return None
for i in range(len(self.que)-1):
self.que.append(self.que.popleft())
return self.que.popleft()
def top(self) -> int:
if self.empty():
return None
return self.que[-1]
def empty(self) -> bool:
return not self.que
```
Go

View File

@ -32,7 +32,7 @@
## 思路
为了满足更多的小孩,就不要造成饼干尺寸的浪费。
为了满足更多的小孩,就不要造成饼干尺寸的浪费。
大尺寸的饼干既可以满足胃口大的孩子也可以满足胃口小的孩子,那么就应该优先满足胃口大的。

View File

@ -157,7 +157,7 @@ class Solution {
```python
class Solution:
def lemonadeChange(self, bills: List[int]) -> bool:
five, ten, twenty = 0, 0, 0
five, ten = 0, 0
for bill in bills:
if bill == 5:
five += 1
@ -169,10 +169,8 @@ class Solution:
if ten > 0 and five > 0:
ten -= 1
five -= 1
twenty += 1
elif five > 2:
five -= 3
twenty += 1
else:
return False
return True

View File

@ -106,22 +106,15 @@ class Solution {
```python
class Solution:
def validMountainArray(self, arr: List[int]) -> bool:
if len(arr) < 3 :
return False
i = 1
flagIncrease = False # 上升
flagDecrease = False # 下降
while i < len(arr) and arr[i-1] < arr[i]:
flagIncrease = True
i += 1
while i < len(arr) and arr[i-1] > arr[i]:
flagDecrease = True
i += 1
return i == len(arr) and flagIncrease and flagDecrease
left, right = 0, len(arr)-1
while left < len(arr)-1 and arr[left+1] > arr[left]:
left += 1
while right > 0 and arr[right-1] > arr[right]:
right -= 1
return left == right and right != 0 and left != len(arr)-1
```