mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
Merge branch 'master' of github.com:flames519/leetcode-master
This commit is contained in:
@ -247,8 +247,23 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, x):
|
||||
# self.val = x
|
||||
# self.left = None
|
||||
# self.right = None
|
||||
|
||||
|
||||
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
|
||||
```
|
||||
Go:
|
||||
|
||||
|
||||
|
@ -263,8 +263,24 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, x):
|
||||
# self.val = x
|
||||
# self.left = None
|
||||
# self.right = None
|
||||
//递归
|
||||
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 //没找到
|
||||
```
|
||||
Go:
|
||||
```Go
|
||||
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
|
||||
|
@ -399,6 +399,49 @@ char ** findItinerary(char *** tickets, int ticketsSize, int* ticketsColSize, in
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
```Javascript
|
||||
|
||||
var findItinerary = function(tickets) {
|
||||
let result = ['JFK']
|
||||
let map = {}
|
||||
|
||||
for (const tickt of tickets) {
|
||||
const [from, to] = tickt
|
||||
if (!map[from]) {
|
||||
map[from] = []
|
||||
}
|
||||
map[from].push(to)
|
||||
}
|
||||
|
||||
for (const city in map) {
|
||||
// 对到达城市列表排序
|
||||
map[city].sort()
|
||||
}
|
||||
function backtracing() {
|
||||
if (result.length === tickets.length + 1) {
|
||||
return true
|
||||
}
|
||||
if (!map[result[result.length - 1]] || !map[result[result.length - 1]].length) {
|
||||
return false
|
||||
}
|
||||
for(let i = 0 ; i < map[result[result.length - 1]].length; i++) {
|
||||
let city = map[result[result.length - 1]][i]
|
||||
// 删除已走过航线,防止死循环
|
||||
map[result[result.length - 1]].splice(i, 1)
|
||||
result.push(city)
|
||||
if (backtracing()) {
|
||||
return true
|
||||
}
|
||||
result.pop()
|
||||
map[result[result.length - 1]].splice(i, 0, city)
|
||||
}
|
||||
}
|
||||
backtracing()
|
||||
return result
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
|
@ -212,7 +212,19 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
|
||||
if len(intervals) == 0: return 0
|
||||
intervals.sort(key=lambda x: x[1])
|
||||
count = 1 # 记录非交叉区间的个数
|
||||
end = intervals[0][1] # 记录区间分割点
|
||||
for i in range(1, len(intervals)):
|
||||
if end <= intervals[i][0]:
|
||||
count += 1
|
||||
end = intervals[i][1]
|
||||
return len(intervals) - count
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
@ -281,7 +281,43 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def deleteNode(self, root: TreeNode, key: int) -> TreeNode:
|
||||
if not root: return root #第一种情况:没找到删除的节点,遍历到空节点直接返回了
|
||||
if root.val == key:
|
||||
if not root.left and not root.right: #第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
|
||||
del root
|
||||
return None
|
||||
if not root.left and root.right: #第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点
|
||||
tmp = root
|
||||
root = root.right
|
||||
del tmp
|
||||
return root
|
||||
if root.left and not root.right: #第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
|
||||
tmp = root
|
||||
root = root.left
|
||||
del tmp
|
||||
return root
|
||||
else: #第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置
|
||||
v = root.right
|
||||
while v.left:
|
||||
v = v.left
|
||||
v.left = root.left
|
||||
tmp = root
|
||||
root = root.right
|
||||
del tmp
|
||||
return root
|
||||
if root.val > key: root.left = self.deleteNode(root.left,key) #左递归
|
||||
if root.val < key: root.right = self.deleteNode(root.right,key) #右递归
|
||||
return root
|
||||
```
|
||||
|
||||
Go:
|
||||
```Go
|
||||
|
@ -70,7 +70,7 @@
|
||||
|
||||
其实都可以!只不过对应的遍历顺序不同,我就按照气球的起始位置排序了。
|
||||
|
||||
既然按照其实位置排序,那么就从前向后遍历气球数组,靠左尽可能让气球重复。
|
||||
既然按照起始位置排序,那么就从前向后遍历气球数组,靠左尽可能让气球重复。
|
||||
|
||||
从前向后遍历遇到重叠的气球了怎么办?
|
||||
|
||||
@ -167,7 +167,19 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def findMinArrowShots(self, points: List[List[int]]) -> int:
|
||||
if len(points) == 0: return 0
|
||||
points.sort(key=lambda x: x[0])
|
||||
result = 1
|
||||
for i in range(1, len(points)):
|
||||
if points[i][0] > points[i - 1][1]: # 气球i和气球i-1不挨着,注意这里不是>=
|
||||
result += 1
|
||||
else:
|
||||
points[i][1] = min(points[i - 1][1], points[i][1]) # 更新重叠气球最小右边界
|
||||
return result
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
@ -225,7 +225,7 @@ public:
|
||||
|
||||
是的,如果仅仅是求个数的话,就可以用dp,但[回溯算法:39. 组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)要求的是把所有组合列出来,还是要使用回溯法爆搜的。
|
||||
|
||||
本地还是有点难度,大家也可以记住,在求装满背包有几种方法的情况下,递推公式一般为:
|
||||
本题还是有点难度,大家也可以记住,在求装满背包有几种方法的情况下,递推公式一般为:
|
||||
|
||||
```
|
||||
dp[j] += dp[j - nums[i]];
|
||||
|
@ -394,8 +394,39 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
//递归法
|
||||
class Solution:
|
||||
def findMode(self, root: TreeNode) -> List[int]:
|
||||
if not root: return
|
||||
self.pre = root
|
||||
self.count = 0 //统计频率
|
||||
self.countMax = 0 //最大频率
|
||||
self.res = []
|
||||
def findNumber(root):
|
||||
if not root: return None // 第一个节点
|
||||
findNumber(root.left) //左
|
||||
if self.pre.val == root.val: //中: 与前一个节点数值相同
|
||||
self.count += 1
|
||||
else: // 与前一个节点数值不同
|
||||
self.pre = root
|
||||
self.count = 1
|
||||
if self.count > self.countMax: // 如果计数大于最大值频率
|
||||
self.countMax = self.count // 更新最大频率
|
||||
self.res = [root.val] //更新res
|
||||
elif self.count == self.countMax: // 如果和最大值相同,放进res中
|
||||
self.res.append(root.val)
|
||||
findNumber(root.right) //右
|
||||
return
|
||||
findNumber(root)
|
||||
return self.res
|
||||
```
|
||||
Go:
|
||||
|
||||
|
||||
|
@ -207,6 +207,19 @@ class Solution:
|
||||
```
|
||||
|
||||
Go:
|
||||
```Go
|
||||
func fib(n int) int {
|
||||
if n < 2 {
|
||||
return n
|
||||
}
|
||||
a, b, c := 0, 1, 0
|
||||
for i := 1; i < n; i++ {
|
||||
c = a + b
|
||||
a, b = b, c
|
||||
}
|
||||
return c
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
@ -228,7 +228,23 @@ Python:
|
||||
|
||||
|
||||
Go:
|
||||
```Go
|
||||
func minCostClimbingStairs(cost []int) int {
|
||||
dp := make([]int, len(cost))
|
||||
dp[0], dp[1] = cost[0], cost[1]
|
||||
for i := 2; i < len(cost); i++ {
|
||||
dp[i] = min(dp[i-1], dp[i-2]) + cost[i]
|
||||
}
|
||||
return min(dp[len(cost)-1], dp[len(cost)-2])
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
@ -122,6 +122,8 @@ public:
|
||||
|
||||
|
||||
Java:
|
||||
|
||||
使用 Deque 作为堆栈
|
||||
```Java
|
||||
class Solution {
|
||||
public String removeDuplicates(String S) {
|
||||
@ -144,6 +146,30 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
拿字符串直接作为栈,省去了栈还要转为字符串的操作。
|
||||
```Java
|
||||
class Solution {
|
||||
public String removeDuplicates(String s) {
|
||||
// 将 res 当做栈
|
||||
StringBuffer res = new StringBuffer();
|
||||
// top为 res 的长度
|
||||
int top = -1;
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
// 当 top > 0,即栈中有字符时,当前字符如果和栈中字符相等,弹出栈顶字符,同时 top--
|
||||
if (top >= 0 && res.charAt(top) == c) {
|
||||
res.deleteCharAt(top);
|
||||
top--;
|
||||
// 否则,将该字符 入栈,同时top++
|
||||
} else {
|
||||
res.append(c);
|
||||
top++;
|
||||
}
|
||||
}
|
||||
return res.toString();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
```python3
|
||||
|
@ -306,6 +306,59 @@ var postorderTraversal = function(root, res = []) {
|
||||
return res;
|
||||
};
|
||||
```
|
||||
Javascript版本:
|
||||
|
||||
前序遍历:
|
||||
```Javascript
|
||||
var preorderTraversal = function(root) {
|
||||
let res=[];
|
||||
const dfs=function(root){
|
||||
if(root===null)return ;
|
||||
//先序遍历所以从父节点开始
|
||||
res.push(root.val);
|
||||
//递归左子树
|
||||
dfs(root.left);
|
||||
//递归右子树
|
||||
dfs(root.right);
|
||||
}
|
||||
//只使用一个参数 使用闭包进行存储结果
|
||||
dfs(root);
|
||||
return res;
|
||||
};
|
||||
```
|
||||
中序遍历
|
||||
```javascript
|
||||
var inorderTraversal = function(root) {
|
||||
let res=[];
|
||||
const dfs=function(root){
|
||||
if(root===null){
|
||||
return ;
|
||||
}
|
||||
dfs(root.left);
|
||||
res.push(root.val);
|
||||
dfs(root.right);
|
||||
}
|
||||
dfs(root);
|
||||
return res;
|
||||
};
|
||||
```
|
||||
|
||||
后序遍历
|
||||
```javascript
|
||||
var postorderTraversal = function(root) {
|
||||
let res=[];
|
||||
const dfs=function(root){
|
||||
if(root===null){
|
||||
return ;
|
||||
}
|
||||
dfs(root.left);
|
||||
dfs(root.right);
|
||||
res.push(root.val);
|
||||
}
|
||||
dfs(root);
|
||||
return res;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user