Skip to content

Commit e03c743

Browse files
committed
更新题解
1 parent 98dc976 commit e03c743

File tree

70 files changed

+4347
-2454
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+4347
-2454
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ collect some good articles about data structure and algorithm.
1010

1111
## 简介
1212

13-
力扣常见考察的知识点大概有十几种,包括:**二分,滑动窗口,双指针,单调栈(单调队列),链表,二叉树,字符串处理,dfs + 回溯,并查集,动态规划,贪心,位运算,数论(质数、约数、欧拉函数、欧几里得算法、中国剩余定理、简单博弈论等),图论(dfs、bfs、flood fill、拓扑排序、二分染色、最短路、最小生成树等)**
13+
力扣常见考察的知识点大概有十几种,包括:**二分,滑动窗口,双指针,单调栈(单调队列),链表,二叉树,字符串处理,dfs + 回溯,并查集,动态规划,贪心,位运算,
14+
数论(质数、约数、欧拉函数、欧几里得算法、中国剩余定理、简单博弈论等),图论(dfs、bfs、flood fill、拓扑排序、二分染色、最短路、最小生成树等)**
1415

1516
系统性地学习算法会有事半功倍的效果,于是我归纳了一些基础算法的知识点,并对力扣上的相关题目进行了整理
1617

@@ -20,7 +21,7 @@ collect some good articles about data structure and algorithm.
2021

2122
[我的力扣主页](https://leetcode-cn.com/u/muyids/)
2223

23-
访问[我的电子书《从零开始学算法》](http://muyids.com/alg/),获取更好的阅读体验
24+
访问[我的电子书《从零开始学算法》](https://muyids.github.io/alg/),获取更好的阅读体验
2425

2526
## ♨️ 导图
2627

@@ -49,17 +50,16 @@ collect some good articles about data structure and algorithm.
4950
└── TOC-By-Tag.md -- 按分类查看目录
5051
```
5152

52-
 
53-
53+
## 工具
5454

55-
## 🔐 Problems & Solutions
5655

57-
完成进度([834](./TOC-By-ID.md)🔑/ [1770](https://leetcode-cn.com/problemset/all/)🔒)
56+
 
5857

58+
## 🔐 Problems & Solutions
5959

60+
完成进度([886](./TOC-By-ID.md)🔑/ [2385](https://leetcode-cn.com/problemset/all/)🔒)
6061

6162
- 🔗 [标签查找](./TOC-By-Tag.md)
6263

6364
- 🔗 [题号查找](./TOC-By-ID.md)
6465

65-

TOC-By-ID.md

Lines changed: 531 additions & 513 deletions
Large diffs are not rendered by default.

TOC-By-Tag.md

Lines changed: 1830 additions & 828 deletions
Large diffs are not rendered by default.

algorithms/1-100/69.sqrtx.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1+
## 解题思路
12

3+
两种二分模板的选择
24

3-
### 解题思路
5+
[l, mid-1], [mid, r][l, mid], [mid+1, r]
46

5-
二分模板
6-
7-
[l, mid -1], [mid, r]
8-
9-
10-
### 代码
7+
## 代码
118

129
```cpp
1310
class Solution {

algorithms/1-100/74.search-a-2d-matrix.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2+
13
编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:
24

35
每行中的整数从左到右按升序排列。
Lines changed: 35 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,46 @@
1-
2-
## 深度优先搜索
3-
4-
分析
5-
6-
递归所有节点满足,二叉树的左孩子 < 根, 右孩子 > 根
7-
8-
代码
9-
10-
```javascript
11-
var isValidBST = function(root) {
12-
13-
function dfs(root){
14-
if (root == null){
15-
return true
16-
}
17-
if (root.left){
18-
if (root.left.val >= root.val) return false
19-
}
20-
if (root.right){
21-
if (root.right.val <= root.val) return false
1+
## 中序遍历-非递归
2+
3+
4+
```java
5+
class Solution {
6+
long tmp = Long.MIN_VALUE;
7+
public boolean isValidBST(TreeNode p) {
8+
if (null == p) return true;
9+
Deque<TreeNode> stk = new LinkedList<>();
10+
while (p != null || !stk.isEmpty()){
11+
while (p != null) { // 先把左子树的左孩子依次进栈
12+
stk.addLast(p);
13+
p = p.left;
14+
}
15+
p = stk.removeLast(); // 访问根节点
16+
if (p.val <=tmp) return false;
17+
tmp = p.val;
18+
p = p.right; // 下一轮右孩子的左边孩子依次入栈
2219
}
23-
24-
return dfs(root.left) && dfs(root.right)
20+
return true;
2521
}
26-
27-
return dfs(root)
28-
};
22+
}
2923
```
3024

31-
但是这样也不一定正确,如下面的case就不通过
25+
## 中序遍历-递归
3226

33-
[10,5,15,null,null,6,20] 返回true,期望false
27+
```java
28+
class Solution {
29+
long tmp = Long.MIN_VALUE;
3430

35-
实际上BST,需要整个右子树都大于根,整个左子树都小于根
36-
37-
前面已经判断了,右孩子一定大于根,左孩子一定小于根;
38-
39-
需要再附加条件:**任意节点的值必须大于其左子树的最右节点;同时小于右子树的最左节点。从根节点开始检查,一旦发现不满足则返回false。**
40-
41-
整理代码
42-
43-
```javascript
44-
var isValidBST = function(root) {
45-
46-
function dfs(root){
47-
if (root == null ){
48-
return true
49-
}
50-
if (root.left){
51-
if (root.left.val >= root.val) return false
52-
let rightest = getRightest(root.left)
53-
if (rightest && root.val <= rightest.val) return false
54-
55-
}
56-
if (root.right){
57-
if (root.right.val <= root.val) return false
58-
let leftest = getLeftest(root.right)
59-
if (leftest && root.val >= leftest.val) return false
60-
}
61-
62-
return dfs(root.left) && dfs(root.right)
63-
}
64-
65-
function getRightest(node){
66-
while (node && node.right) node = node.right
67-
return node
68-
}
69-
70-
function getLeftest(node){
71-
while (node && node.left) node = node.left
72-
return node
73-
}
74-
75-
return dfs(root)
76-
};
77-
```
78-
79-
自上往下递归,传入当前子树所有值应该在的区间
80-
81-
x [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]
82-
y z
83-
m n p q
84-
85-
y取值 [Number.MIN_SAFE_INTEGER, x-1]
86-
z取值 [x+1, Number.MAX_SAFE_INTEGER]
87-
依次递推
88-
89-
```javascript
90-
var isValidBST = function(root) {
91-
function dfs(root, low, high){
92-
if (root == null) return true
93-
if (root.left && root.left.val >= root.val)return false
94-
if (root.right && root.right.val <= root.val)return false
95-
if (root.left && root.left.val < low) return false
96-
if (root.right && root.right.val > high) return false
97-
return dfs(root.left, low, root.val-1) && dfs(root.right, root.val+1, high)
31+
public boolean isValidBST(TreeNode p) {
32+
if (null == p) return true;
33+
boolean left = isValidBST(p.left);
34+
if (p.val <= tmp) return false;
35+
tmp = p.val;
36+
boolean right = isValidBST(p.right);
37+
return left && right;
9838
}
99-
return dfs(root, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER)
100-
};
101-
39+
}
10240
```
10341

104-
### 中序遍历
42+
## 二叉搜索树的性质
10543

106-
```javascript
107-
var isValidBST = function(root) {
108-
var queue = []
109-
function dfs(root){
110-
if (!root) return
111-
if (root.left) dfs(root.left)
112-
if (root) queue.push(root.val)
113-
if (root.right) dfs(root.right)
114-
}
44+
二叉搜索树的性质是: 左子树最右节点 < 根 < 右子树的最左节点
11545

116-
dfs(root)
117-
118-
for (let i =0; i< queue.length-1; i++){
119-
if (queue[i] >= queue[i+1]) return false
120-
}
121-
122-
return true
123-
};
124-
```
46+
缺点:两次遍历,性能差

algorithms/101-200/111.minimum-depth-of-binary-tree.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,36 @@
2121

2222
### bfs
2323

24+
25+
26+
java
27+
28+
```java
29+
class Solution {
30+
public int minDepth(TreeNode root) {
31+
if (root == null) return 0;
32+
Queue<TreeNode> q = new ArrayDeque<>();
33+
int floor = 0;
34+
q.add(root);
35+
while (!q.isEmpty()) {
36+
floor++;
37+
int k = q.size();
38+
while (k-- > 0) {
39+
TreeNode top = q.poll();
40+
if (top.left == null && top.right == null) return floor;
41+
if (top.left != null) q.add(top.left);
42+
if (top.right!=null) q.add(top.right);
43+
}
44+
}
45+
return -1;
46+
}
47+
}
48+
```
49+
50+
51+
52+
c++
53+
2454
```cpp
2555
class Solution {
2656
public:
@@ -48,6 +78,24 @@ public:
4878
4979
### dfs
5080
81+
Java
82+
83+
```java
84+
class Solution {
85+
public int minDepth(TreeNode root) {
86+
if (root== null) return 0;
87+
int left = minDepth(root.left);
88+
int right = minDepth(root.right);
89+
if (left == 0 || right == 0) return left+right + 1;
90+
return Math.min(left, right) + 1;
91+
}
92+
}
93+
```
94+
95+
96+
97+
c++
98+
5199
```cpp
52100
class Solution {
53101
public:

0 commit comments

Comments
 (0)