Merge branch 'youngyangyang04:master' into master

This commit is contained in:
yqq
2021-09-20 15:59:40 +08:00
committed by GitHub
5 changed files with 92 additions and 15 deletions

View File

@ -368,6 +368,34 @@ class Solution {
}
```
Python2:
```python
class Solution(object):
def combine(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[List[int]]
"""
result = []
path = []
def backtracking(n, k, startidx):
if len(path) == k:
result.append(path[:])
return
# 剪枝, 最后k - len(path)个节点直接构造结果,无需递归
last_startidx = n - (k - len(path)) + 1
result.append(path + [idx for idx in range(last_startidx, n + 1)])
for x in range(startidx, last_startidx):
path.append(x)
backtracking(n, k, x + 1) # 递归
path.pop() # 回溯
backtracking(n, k, 1)
return result
```
## Python
```python

View File

@ -185,7 +185,8 @@ public:
queue<TreeNode*> que;
que.push(root->left); // 将左子树头结点加入队列
que.push(root->right); // 将右子树头结点加入队列
while (!que.empty()) { // 接下来就要判断这这两个树是否相互翻转
while (!que.empty()) { // 接下来就要判断这两个树是否相互翻转
TreeNode* leftNode = que.front(); que.pop();
TreeNode* rightNode = que.front(); que.pop();
if (!leftNode && !rightNode) { // 左节点为空、右节点为空,此时说明是对称的

View File

@ -268,17 +268,30 @@ class Solution {
递归法:
```python
class Solution:
"""二叉搜索树的最近公共祖先 递归法"""
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if not root: return root //
if root.val >p.val and root.val > q.val:
return self.lowestCommonAncestor(root.left,p,q) //
elif root.val < p.val and root.val < q.val:
return self.lowestCommonAncestor(root.right,p,q) //
else: return root
if root.val > p.val and root.val > q.val:
return self.lowestCommonAncestor(root.left, p, q)
if root.val < p.val and root.val < q.val:
return self.lowestCommonAncestor(root.right, p, q)
return root
```
迭代法:
```python
class Solution:
"""二叉搜索树的最近公共祖先 迭代法"""
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
while True:
if root.val > p.val and root.val > q.val:
root = root.left
elif root.val < p.val and root.val < q.val:
root = root.right
else:
return root
```
## Go

View File

@ -264,16 +264,21 @@ class Solution {
## Python
```python
//递归
class Solution:
"""二叉树的最近公共祖先 递归法"""
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if not root or root == p or root == q: return root //找到了节点p或者q或者遇到空节点
left = self.lowestCommonAncestor(root.left,p,q) //
right = self.lowestCommonAncestor(root.right,p,q) //
if left and right: return root //: left和right不为空root就是最近公共节点
elif left and not right: return left //目标节点是通过left返回的
elif not left and right: return right //目标节点是通过right返回的
else: return None //没找到
if not root or root == p or root == q:
return root
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)
if left and right:
return root
if left:
return left
return right
```
## Go

View File

@ -276,6 +276,36 @@ func dailyTemperatures(num []int) []int {
}
```
JavaScript
```javascript
/**
* @param {number[]} temperatures
* @return {number[]}
*/
var dailyTemperatures = function(temperatures) {
let n = temperatures.length;
let res = new Array(n).fill(0);
let stack = []; // 递减栈:用于存储元素右面第一个比他大的元素下标
stack.push(0);
for (let i = 1; i < n; i++) {
// 栈顶元素
let top = stack[stack.length - 1];
if (temperatures[i] < temperatures[top]) {
stack.push(i);
} else if (temperatures[i] === temperatures[top]) {
stack.push(i);
} else {
while (stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) {
let top = stack.pop();
res[top] = i - top;
}
stack.push(i);
}
}
return res;
};
```
-----------------------