Skip to content

Commit 4869546

Browse files
committed
新增题解
1 parent cdd17e4 commit 4869546

File tree

12 files changed

+873
-3
lines changed

12 files changed

+873
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
## 简介
99

10-
- LeetCode**第二多的官方认证精选题解**,通过手写演算和绘画让算法更易理解。
10+
- 力扣**精选题解**,通过手写演算和绘画让算法更易理解。
1111
- 文章首发于微信公众号「[牧码啦](https://i.loli.net/2019/05/20/5ce23b33cc01d73486.gif)」,您可以关注后获取文章更新。
1212
- 每日更新一题,**欢迎关注后加入天天算法群**,一起打卡学算法。
1313
- 文章发布公众号后,会同步更新[力扣](https://leetcode-cn.com/)题解和本项目的[在线阅读](https://guanpengchn.github.io/LeetCodeDrawing/)

docs/.vuepress/config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ const algorithmArr = [
88
'9',
99
'13',
1010
'14',
11+
'15',
1112
'16',
1213
'19',
1314
'21',
1415
'24',
1516
'27',
1617
'35',
1718
'53',
19+
'58',
1820
'66',
1921
'67',
2022
'70',
@@ -23,11 +25,13 @@ const algorithmArr = [
2325
'96',
2426
'101',
2527
'104',
28+
'111',
2629
'112',
2730
'136',
2831
'152',
2932
'162',
3033
'171',
34+
'198',
3135
'216',
3236
'242',
3337
'520',
@@ -54,7 +58,9 @@ const openArr = [
5458
'杜兰特受伤,后果自己承担',
5559
'Java看书路径',
5660
'技术人员的简历如何排版',
57-
'蚂蚁金服读书感'
61+
'蚂蚁金服读书感',
62+
'牛客访谈',
63+
'FlipaClip——人人都是灵魂画师'
5864
]
5965

6066
module.exports = {

docs/.vuepress/dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Subproject commit 9d496f325922756268b95f8ea2ea9caef2d93e20
1+
Subproject commit 645a05bac4ecc7be52aec91994fd5a9a3310e4ad

docs/algorithm/111.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# 画解算法:111. 二叉树的最小深度
2+
3+
## 题目链接
4+
5+
https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
6+
7+
## 题目描述
8+
9+
给定一个二叉树,找出其最小深度。
10+
11+
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
12+
13+
说明: 叶子节点是指没有子节点的节点。
14+
15+
示例:
16+
17+
给定二叉树 `[3,9,20,null,null,15,7]`,
18+
19+
```bash
20+
3
21+
/ \
22+
9 20
23+
/ \
24+
15 7
25+
```
26+
27+
返回它的最小深度  `2`.
28+
29+
## 解题方案
30+
31+
### 思路
32+
33+
- 标签:DFS
34+
- 终止条件、返回值和递归过程:
35+
- 当前节点root为空时,说明此处树的高度为0,0也是最小值
36+
- 当前节点root的左子树和右子树都为空时,说明此处树的高度为1,1也是最小值
37+
- 如果为其他情况,则说明当前节点有值,且需要分别计算其左右子树的最小深度,返回最小深度+1,+1表示当前节点存在有1个深度
38+
- 时间复杂度:O(n),n为树的节点数量
39+
40+
### 代码
41+
42+
- Java版本
43+
44+
```Java
45+
/**
46+
* Definition for a binary tree node.
47+
* public class TreeNode {
48+
* int val;
49+
* TreeNode left;
50+
* TreeNode right;
51+
* TreeNode(int x) { val = x; }
52+
* }
53+
*/
54+
class Solution {
55+
public int minDepth(TreeNode root) {
56+
if(root == null) {
57+
return 0;
58+
}
59+
if(root.left == null && root.right == null) {
60+
return 1;
61+
}
62+
int ans = Integer.MAX_VALUE;
63+
if(root.left != null) {
64+
ans = Math.min(minDepth(root.left), ans);
65+
}
66+
if(root.right != null) {
67+
ans = Math.min(minDepth(root.right), ans);
68+
}
69+
return ans + 1;
70+
}
71+
}
72+
```
73+
74+
- JavaScript版本
75+
76+
```JavaScript
77+
/**
78+
* Definition for a binary tree node.
79+
* function TreeNode(val) {
80+
* this.val = val;
81+
* this.left = this.right = null;
82+
* }
83+
*/
84+
/**
85+
* @param {TreeNode} root
86+
* @return {number}
87+
*/
88+
var minDepth = function(root) {
89+
if(root == null) {
90+
return 0;
91+
}
92+
if(root.left == null && root.right == null) {
93+
return 1;
94+
}
95+
let ans = Number.MAX_SAFE_INTEGER;
96+
if(root.left != null) {
97+
ans = Math.min(minDepth(root.left), ans);
98+
}
99+
if(root.right != null) {
100+
ans = Math.min(minDepth(root.right), ans);
101+
}
102+
return ans + 1;
103+
};
104+
```
105+
106+
107+
### 画解
108+
109+
![fr<x>ame_00001.png](https://i.loli.net/2019/07/01/5d1966cde681f79871.png)
110+
![fr<x>ame_00002.png](https://i.loli.net/2019/07/01/5d1966cdd983b28744.png)
111+
![fr<x>ame_00003.png](https://i.loli.net/2019/07/01/5d1966ce3370e83043.png)
112+
![fr<x>ame_00004.png](https://i.loli.net/2019/07/01/5d1966ce5619d14737.png)
113+
114+
115+
<span style="display:block;text-align:center;">后台回复「<strong>算法</strong>」,加入天天算法群</span>
116+
<span style="display:block;text-align:center;">觉得算法直击灵魂,欢迎点击<strong>在看</strong>和<strong>转发</strong></span>
117+
118+
![](https://i.loli.net/2019/05/20/5ce23b33cc01d73486.gif)

docs/algorithm/15.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# 画解算法:15. 三数之和
2+
3+
## 题目链接
4+
5+
https://leetcode-cn.com/problems/3sum/
6+
7+
## 题目描述
8+
9+
给定一个包含 `n` 个整数的数组 `nums`,判断 `nums` 中是否存在三个元素 `a,b,c` ,使得 `a + b + c = 0` ?找出所有满足条件且不重复的三元组。
10+
11+
注意:答案中不可以包含重复的三元组。
12+
13+
```bash
14+
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
15+
16+
满足要求的三元组集合为:
17+
[
18+
[-1, 0, 1],
19+
[-1, -1, 2]
20+
]
21+
```
22+
23+
## 解题方案
24+
25+
### 思路
26+
27+
- 标签:数组遍历
28+
- 首先对数组进行排序,排序后固定一个数nums[i],再使用左右指针指向nums[i]后面的两端,数字分别为nums[L]和nums[R],计算三个数的和sum判断是否满足为0,满足则添加进结果集
29+
- 如果nums[i]大于0,则三数之和必然无法等于0,结束循环
30+
- 如果nums[i] == nums[i-1],则说明该数字重复,会导致结果重复,所以应该跳过
31+
- 当sum == 0时,nums[L] == nums[L+1]则会导致结果重复,应该跳过,L++
32+
- 当sum == 0时,nums[R] == nums[R-1]则会导致结果重复,应该跳过,R--
33+
- 时间复杂度:O(n^2),n为数组长度
34+
35+
### 代码
36+
37+
- Java版本
38+
39+
```Java
40+
class Solution {
41+
public static List<List<Integer>> threeSum(int[] nums) {
42+
List<List<Integer>> ans = new ArrayList();
43+
Arrays.sort(nums); // 排序
44+
int len = nums.length;
45+
if(nums == null || len < 3) return ans;
46+
for (int i = 0; i < len ; i++) {
47+
if(nums[i] > 0) break; // 如果当前数字大于0,则三数之和一定大于0,所以结束循环
48+
if(i > 0 && nums[i] == nums[i-1]) continue; // 去重
49+
int L = i+1;
50+
int R = len-1;
51+
while(L < R){
52+
int sum = nums[i] + nums[L] + nums[R];
53+
if(sum == 0){
54+
ans.add(Arrays.asList(nums[i],nums[L],nums[R]));
55+
while (L<R && nums[L] == nums[L+1]) L++; // 去重
56+
while (L<R && nums[R] == nums[R-1]) R--; // 去重
57+
L++;
58+
R--;
59+
}
60+
else if (sum < 0) L++;
61+
else if (sum > 0) R--;
62+
}
63+
}
64+
return ans;
65+
}
66+
}
67+
```
68+
69+
- JavaScript版本
70+
71+
```JavaScript
72+
/**
73+
* @param {number[]} nums
74+
* @return {number[][]}
75+
*/
76+
var threeSum = function(nums) {
77+
let ans = [];
78+
nums.sort((a, b) => a - b); // 排序
79+
const len = nums.length;
80+
if(nums == null || len < 3) return ans;
81+
for (let i = 0; i < len ; i++) {
82+
if(nums[i] > 0) break; // 如果当前数字大于0,则三数之和一定大于0,所以结束循环
83+
if(i > 0 && nums[i] == nums[i-1]) continue; // 去重
84+
let L = i+1;
85+
let R = len-1;
86+
while(L < R){
87+
const sum = nums[i] + nums[L] + nums[R];
88+
if(sum == 0){
89+
ans.push([nums[i],nums[L],nums[R]]);
90+
while (L<R && nums[L] == nums[L+1]) L++; // 去重
91+
while (L<R && nums[R] == nums[R-1]) R--; // 去重
92+
L++;
93+
R--;
94+
}
95+
else if (sum < 0) L++;
96+
else if (sum > 0) R--;
97+
}
98+
}
99+
return ans;
100+
};
101+
```
102+
103+
104+
### 画解
105+
106+
![0.png](https://i.loli.net/2019/07/04/5d1d5e2d4008b86591.png)
107+
![1.png](https://i.loli.net/2019/07/04/5d1d5e2d4d7d043319.png)
108+
![2.png](https://i.loli.net/2019/07/04/5d1d5e2d4c1f686472.png)
109+
![3.png](https://i.loli.net/2019/07/04/5d1d5e2d748a243103.png)
110+
![4.png](https://i.loli.net/2019/07/04/5d1d5e2d807e050456.png)
111+
![5.png](https://i.loli.net/2019/07/04/5d1d5e2d772a043887.png)
112+
![6.png](https://i.loli.net/2019/07/04/5d1d5e2f562b514756.png)
113+
![7.png](https://i.loli.net/2019/07/04/5d1d5e2f5d2c099716.png)
114+
![8.png](https://i.loli.net/2019/07/04/5d1d5e2f59c0f19934.png)
115+
![9.png](https://i.loli.net/2019/07/04/5d1d5ec553dae11391.png)
116+
![10.png](https://i.loli.net/2019/07/04/5d1d5f17e323883978.png)
117+
![11.png](https://i.loli.net/2019/07/04/5d1d5f1fe8aa224651.png)
118+
![12.png](https://i.loli.net/2019/07/04/5d1d6102cf00485151.png)
119+
120+
121+
<span style="display:block;text-align:center;">后台回复「<strong>算法</strong>」,加入天天算法群</span>
122+
<span style="display:block;text-align:center;">觉得算法直击灵魂,欢迎点击<strong>在看</strong>和<strong>转发</strong></span>
123+
124+
![](https://i.loli.net/2019/05/20/5ce23b33cc01d73486.gif)

docs/algorithm/198.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# 画解算法:198. 打家劫舍
2+
3+
## 题目链接
4+
5+
https://leetcode-cn.com/problems/house-robber/
6+
7+
## 题目描述
8+
9+
你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,**如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。**
10+
11+
给定一个代表每个房屋存放金额的非负整数数组,计算你在**不触动警报装置的情况下**,能够偷窃到的最高金额。
12+
13+
示例 1:
14+
15+
```bash
16+
输入: [1,2,3,1]
17+
输出: 4
18+
解释: 偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。
19+
  偷窃到的最高金额 = 1 + 3 = 4 。
20+
```
21+
22+
示例 2:
23+
24+
```bash
25+
输入: [2,7,9,3,1]
26+
输出: 12
27+
解释: 偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。
28+
  偷窃到的最高金额 = 2 + 9 + 1 = 12 。
29+
```
30+
31+
## 解题方案
32+
33+
### 思路
34+
35+
- 标签:动态规划
36+
- 动态规划方程:`dp[n] = MAX( dp[n-1], dp[n-2] + num )`
37+
- 由于不可以在相邻的房屋闯入,所以在当前位置`n`房屋可盗窃的最大值,要么就是`n-1`房屋可盗窃的最大值,要么就是`n-2`房屋可盗窃的最大值加上当前房屋的值,二者之间取最大值
38+
- 举例来说:1号房间可盗窃最大值为3即为`dp[1]=3`,2号房间可盗窃最大值为4即为`dp[2]=4`,3号房间自身的值为2即为`num=2`,那么`dp[3] = MAX( dp[2], dp[1] + num ) = MAX(4, 3+2) = 5`,3号房间可盗窃最大值为5
39+
- 时间复杂度:O(n),n为数组长度
40+
41+
### 代码
42+
43+
- Java版本
44+
45+
```Java
46+
class Solution {
47+
public int rob(int[] nums) {
48+
int len = nums.length;
49+
if(len == 0)
50+
return 0;
51+
int[] dp = new int[len + 1];
52+
dp[0] = 0;
53+
dp[1] = nums[0];
54+
for(int i = 2; i <= len; i++) {
55+
dp[i] = Math.max(dp[i-1], dp[i-2] + nums[i-1]);
56+
}
57+
return dp[len];
58+
}
59+
}
60+
```
61+
62+
- JavaScript版本
63+
64+
```JavaScript
65+
/**
66+
* @param {number[]} nums
67+
* @return {number}
68+
*/
69+
var rob = function(nums) {
70+
const len = nums.length;
71+
if(len == 0)
72+
return 0;
73+
const dp = new Array(len + 1);
74+
dp[0] = 0;
75+
dp[1] = nums[0];
76+
for(let i = 2; i <= len; i++) {
77+
dp[i] = Math.max(dp[i-1], dp[i-2] + nums[i-1]);
78+
}
79+
return dp[len];
80+
};
81+
```
82+
83+
84+
### 画解
85+
86+
![fr&lt;x&gt;ame_00001.png](https://i.loli.net/2019/07/03/5d1c03305cc0272251.png)
87+
![fr&lt;x&gt;ame_00002.png](https://i.loli.net/2019/07/03/5d1c03309467095570.png)
88+
![fr&lt;x&gt;ame_00003.png](https://i.loli.net/2019/07/03/5d1c03308f89c57060.png)
89+
![fr&lt;x&gt;ame_00004.png](https://i.loli.net/2019/07/03/5d1c0332320b416773.png)
90+
![fr&lt;x&gt;ame_00005.png](https://i.loli.net/2019/07/03/5d1c033268f8783549.png)
91+
92+
93+
94+
<span style="display:block;text-align:center;">后台回复「<strong>算法</strong>」,加入天天算法群</span>
95+
<span style="display:block;text-align:center;">觉得算法直击灵魂,欢迎点击<strong>在看</strong>和<strong>转发</strong></span>
96+
97+
![](https://i.loli.net/2019/05/20/5ce23b33cc01d73486.gif)

0 commit comments

Comments
 (0)