Skip to content

Commit 57d80b6

Browse files
committed
Merge branch 'master' of github.com:flames519/leetcode-master
2 parents 2aed976 + 88ec3e6 commit 57d80b6

12 files changed

+290
-17
lines changed

problems/0235.二叉搜索树的最近公共祖先.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,23 @@ class Solution {
247247
```
248248

249249
Python:
250-
251-
250+
```python
251+
# Definition for a binary tree node.
252+
# class TreeNode:
253+
# def __init__(self, x):
254+
# self.val = x
255+
# self.left = None
256+
# self.right = None
257+
258+
class Solution:
259+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
260+
if not root: return root //
261+
if root.val >p.val and root.val > q.val:
262+
return self.lowestCommonAncestor(root.left,p,q) //
263+
elif root.val < p.val and root.val < q.val:
264+
return self.lowestCommonAncestor(root.right,p,q) //
265+
else: return root
266+
```
252267
Go:
253268

254269

@@ -258,4 +273,4 @@ Go:
258273
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
259274
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
260275
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
261-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
276+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

problems/0236.二叉树的最近公共祖先.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,24 @@ class Solution {
263263
```
264264

265265
Python:
266-
267-
266+
```python
267+
# Definition for a binary tree node.
268+
# class TreeNode:
269+
# def __init__(self, x):
270+
# self.val = x
271+
# self.left = None
272+
# self.right = None
273+
//递归
274+
class Solution:
275+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
276+
if not root or root == p or root == q: return root //找到了节点p或者q,或者遇到空节点
277+
left = self.lowestCommonAncestor(root.left,p,q) //
278+
right = self.lowestCommonAncestor(root.right,p,q) //
279+
if left and right: return root //中: left和right不为空,root就是最近公共节点
280+
elif left and not right: return left //目标节点是通过left返回的
281+
elif not left and right: return right //目标节点是通过right返回的
282+
else: return None //没找到
283+
```
268284
Go:
269285
```Go
270286
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {

problems/0332.重新安排行程.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,49 @@ char ** findItinerary(char *** tickets, int ticketsSize, int* ticketsColSize, in
399399
}
400400
```
401401
402+
Javascript:
403+
```Javascript
404+
405+
var findItinerary = function(tickets) {
406+
let result = ['JFK']
407+
let map = {}
408+
409+
for (const tickt of tickets) {
410+
const [from, to] = tickt
411+
if (!map[from]) {
412+
map[from] = []
413+
}
414+
map[from].push(to)
415+
}
416+
417+
for (const city in map) {
418+
// 对到达城市列表排序
419+
map[city].sort()
420+
}
421+
function backtracing() {
422+
if (result.length === tickets.length + 1) {
423+
return true
424+
}
425+
if (!map[result[result.length - 1]] || !map[result[result.length - 1]].length) {
426+
return false
427+
}
428+
for(let i = 0 ; i < map[result[result.length - 1]].length; i++) {
429+
let city = map[result[result.length - 1]][i]
430+
// 删除已走过航线,防止死循环
431+
map[result[result.length - 1]].splice(i, 1)
432+
result.push(city)
433+
if (backtracing()) {
434+
return true
435+
}
436+
result.pop()
437+
map[result[result.length - 1]].splice(i, 0, city)
438+
}
439+
}
440+
backtracing()
441+
return result
442+
};
443+
444+
```
402445

403446
-----------------------
404447
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

problems/0435.无重叠区间.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,19 @@ class Solution {
212212
```
213213

214214
Python:
215-
215+
```python
216+
class Solution:
217+
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
218+
if len(intervals) == 0: return 0
219+
intervals.sort(key=lambda x: x[1])
220+
count = 1 # 记录非交叉区间的个数
221+
end = intervals[0][1] # 记录区间分割点
222+
for i in range(1, len(intervals)):
223+
if end <= intervals[i][0]:
224+
count += 1
225+
end = intervals[i][1]
226+
return len(intervals) - count
227+
```
216228

217229
Go:
218230

@@ -223,4 +235,4 @@ Go:
223235
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
224236
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
225237
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
226-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
238+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

problems/0450.删除二叉搜索树中的节点.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,43 @@ class Solution {
281281
```
282282

283283
Python:
284-
284+
```python
285+
# Definition for a binary tree node.
286+
# class TreeNode:
287+
# def __init__(self, val=0, left=None, right=None):
288+
# self.val = val
289+
# self.left = left
290+
# self.right = right
291+
class Solution:
292+
def deleteNode(self, root: TreeNode, key: int) -> TreeNode:
293+
if not root: return root #第一种情况:没找到删除的节点,遍历到空节点直接返回了
294+
if root.val == key:
295+
if not root.left and not root.right: #第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
296+
del root
297+
return None
298+
if not root.left and root.right: #第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点
299+
tmp = root
300+
root = root.right
301+
del tmp
302+
return root
303+
if root.left and not root.right: #第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
304+
tmp = root
305+
root = root.left
306+
del tmp
307+
return root
308+
else: #第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置
309+
v = root.right
310+
while v.left:
311+
v = v.left
312+
v.left = root.left
313+
tmp = root
314+
root = root.right
315+
del tmp
316+
return root
317+
if root.val > key: root.left = self.deleteNode(root.left,key) #左递归
318+
if root.val < key: root.right = self.deleteNode(root.right,key) #右递归
319+
return root
320+
```
285321

286322
Go:
287323
```Go
@@ -330,4 +366,4 @@ func deleteNode1(root *TreeNode)*TreeNode{
330366
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
331367
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
332368
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
333-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
369+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

problems/0452.用最少数量的箭引爆气球.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070

7171
其实都可以!只不过对应的遍历顺序不同,我就按照气球的起始位置排序了。
7272

73-
既然按照其实位置排序,那么就从前向后遍历气球数组,靠左尽可能让气球重复。
73+
既然按照起始位置排序,那么就从前向后遍历气球数组,靠左尽可能让气球重复。
7474

7575
从前向后遍历遇到重叠的气球了怎么办?
7676

@@ -167,7 +167,19 @@ class Solution {
167167
```
168168

169169
Python:
170-
170+
```python
171+
class Solution:
172+
def findMinArrowShots(self, points: List[List[int]]) -> int:
173+
if len(points) == 0: return 0
174+
points.sort(key=lambda x: x[0])
175+
result = 1
176+
for i in range(1, len(points)):
177+
if points[i][0] > points[i - 1][1]: # 气球i和气球i-1不挨着,注意这里不是>=
178+
result += 1
179+
else:
180+
points[i][1] = min(points[i - 1][1], points[i][1]) # 更新重叠气球最小右边界
181+
return result
182+
```
171183

172184
Go:
173185

@@ -178,4 +190,4 @@ Go:
178190
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
179191
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
180192
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
181-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
193+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

problems/0494.目标和.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public:
225225
226226
是的,如果仅仅是求个数的话,就可以用dp,但[回溯算法:39. 组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)要求的是把所有组合列出来,还是要使用回溯法爆搜的。
227227
228-
本地还是有点难度,大家也可以记住,在求装满背包有几种方法的情况下,递推公式一般为:
228+
本题还是有点难度,大家也可以记住,在求装满背包有几种方法的情况下,递推公式一般为:
229229
230230
```
231231
dp[j] += dp[j - nums[i]];
@@ -272,4 +272,4 @@ Go:
272272
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
273273
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
274274
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
275-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
275+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

problems/0501.二叉搜索树中的众数.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,39 @@ class Solution {
394394
```
395395
396396
Python:
397-
398-
397+
```python
398+
# Definition for a binary tree node.
399+
# class TreeNode:
400+
# def __init__(self, val=0, left=None, right=None):
401+
# self.val = val
402+
# self.left = left
403+
# self.right = right
404+
//递归法
405+
class Solution:
406+
def findMode(self, root: TreeNode) -> List[int]:
407+
if not root: return
408+
self.pre = root
409+
self.count = 0 //统计频率
410+
self.countMax = 0 //最大频率
411+
self.res = []
412+
def findNumber(root):
413+
if not root: return None // 第一个节点
414+
findNumber(root.left) //左
415+
if self.pre.val == root.val: //中: 与前一个节点数值相同
416+
self.count += 1
417+
else: // 与前一个节点数值不同
418+
self.pre = root
419+
self.count = 1
420+
if self.count > self.countMax: // 如果计数大于最大值频率
421+
self.countMax = self.count // 更新最大频率
422+
self.res = [root.val] //更新res
423+
elif self.count == self.countMax: // 如果和最大值相同,放进res中
424+
self.res.append(root.val)
425+
findNumber(root.right) //右
426+
return
427+
findNumber(root)
428+
return self.res
429+
```
399430
Go:
400431

401432

@@ -405,4 +436,4 @@ Go:
405436
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
406437
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
407438
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
408-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
439+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

problems/0509.斐波那契数.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,19 @@ class Solution:
207207
```
208208

209209
Go:
210+
```Go
211+
func fib(n int) int {
212+
if n < 2 {
213+
return n
214+
}
215+
a, b, c := 0, 1, 0
216+
for i := 1; i < n; i++ {
217+
c = a + b
218+
a, b = b, c
219+
}
220+
return c
221+
}
222+
```
210223

211224

212225

problems/0746.使用最小花费爬楼梯.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,23 @@ Python:
228228

229229

230230
Go:
231+
```Go
232+
func minCostClimbingStairs(cost []int) int {
233+
dp := make([]int, len(cost))
234+
dp[0], dp[1] = cost[0], cost[1]
235+
for i := 2; i < len(cost); i++ {
236+
dp[i] = min(dp[i-1], dp[i-2]) + cost[i]
237+
}
238+
return min(dp[len(cost)-1], dp[len(cost)-2])
239+
}
231240

241+
func min(a, b int) int {
242+
if a < b {
243+
return a
244+
}
245+
return b
246+
}
247+
```
232248

233249

234250

0 commit comments

Comments
 (0)