Skip to content

Commit 8c612cc

Browse files
committed
添加题解
1 parent b736d5f commit 8c612cc

File tree

4 files changed

+235
-5
lines changed

4 files changed

+235
-5
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
|周日|周一|周二|周三|周四|周五|周六|
3333
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
3434
|||||||[152](https://mp.weixin.qq.com/s/FvA_wcLq4JU70nXK6lrFHA)|
35-
|[171](https://mp.weixin.qq.com/s/jC4BsJ5r6QixdnwTIchTQw)|[16](https://mp.weixin.qq.com/s/IBEJdeU6G-sBSMuv02LrHg)|[24](https://mp.weixin.qq.com/s/_bjKxIrmtwU0IPtyWtKd_g)|[2](https://mp.weixin.qq.com/s/8cMt_Yaeu6AT5jk3DhdhqA)|[3](https://mp.weixin.qq.com/s/O-2CdSJTFWodVZzGYAjGrA)|
35+
|[171](https://mp.weixin.qq.com/s/jC4BsJ5r6QixdnwTIchTQw)|[16](https://mp.weixin.qq.com/s/IBEJdeU6G-sBSMuv02LrHg)|[24](https://mp.weixin.qq.com/s/_bjKxIrmtwU0IPtyWtKd_g)|[2](https://mp.weixin.qq.com/s/8cMt_Yaeu6AT5jk3DhdhqA)|[3](https://mp.weixin.qq.com/s/O-2CdSJTFWodVZzGYAjGrA)|[7](https://mp.weixin.qq.com/s/wwn4KOz_VBA1l2q6CHPdtA)|[9](https://mp.weixin.qq.com/s/hoBYW2m7oQ2h0UfwVjT5Hw)|
36+
|[19](https://mp.weixin.qq.com/s/qsElRxU6qJML9KQ3nWw9Yg)|[35](https://mp.weixin.qq.com/s/UnuRk-TT133I2OMxuD1hNg)|
3637

3738
## 精选题解
3839

@@ -52,7 +53,7 @@
5253
</a>
5354

5455

55-
<a href="">
56+
<a href="https://mp.weixin.qq.com/s/wwn4KOz_VBA1l2q6CHPdtA">
5657
<img width="24%" height="24%" src="https://i.loli.net/2019/06/05/5cf7854b1d01876390.png"/>
5758
</a>
5859
<a href="https://mp.weixin.qq.com/s/_bjKxIrmtwU0IPtyWtKd_g">
@@ -88,6 +89,13 @@
8889
<img width="24%" height="24%" src="https://i.loli.net/2019/05/26/5cea098e6bb7383654.png"/>
8990
</a>
9091

92+
<a href="https://mp.weixin.qq.com/s/qsElRxU6qJML9KQ3nWw9Yg">
93+
<img width="24%" height="24%" src="https://i.loli.net/2019/06/08/5cfb590b01fc369826.png"/>
94+
</a>
95+
<a href="https://mp.weixin.qq.com/s/UnuRk-TT133I2OMxuD1hNg">
96+
<img width="24%" height="24%" src="https://i.loli.net/2019/06/10/5cfdaa6db660258479.png"/>
97+
</a>
98+
9199
## 在线阅读开发
92100

93101
在线阅读基于VuePress

docs/algorithm/19.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
# 画解算法:19. 删除链表的倒数第N个节点
3+
4+
## 题目链接
5+
6+
https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
7+
8+
## 题目描述
9+
10+
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。
11+
12+
**示例:**
13+
14+
```bash
15+
给定一个链表: 1->2->3->4->5, 和 n = 2.
16+
17+
当删除了倒数第二个节点后,链表变为 1->2->3->5.
18+
```
19+
20+
**说明:**
21+
22+
给定的 `n` 保证是有效的。
23+
24+
**进阶:**
25+
26+
你能尝试使用一趟扫描实现吗?
27+
28+
29+
## 解题方案
30+
31+
### 思路
32+
33+
- 标签:链表
34+
- 整体思路是让前面的指针先移动`n`步,之后前后指针共同移动直到前面的指针到尾部为止
35+
- 首先设立预先指针pre,预先指针是一个小技巧,在[第2题](https://mp.weixin.qq.com/s/8cMt_Yaeu6AT5jk3DhdhqA)中进行了讲解
36+
- 设预先指针`pre`的下一个节点指向`head`,设前指针为`start`,后指针为`end`,二者都等于`pre`
37+
- `start`先向前移动n步
38+
- 之后`start``end`共同向前移动,此时二者的距离为`n`,当`start`到尾部时,`end`的位置恰好为倒数第`n`个节点
39+
- 因为要删除该节点,所以要移动到该节点的前一个才能删除,所以循环结束条件为`start.next != null`
40+
- 删除后返回`pre.next`,为什么不直接返回`head`呢,因为`head`有可能是被删掉的点
41+
- 时间复杂度:O(n)
42+
43+
### 代码
44+
45+
```java
46+
/**
47+
* Definition for singly-linked list.
48+
* public class ListNode {
49+
* int val;
50+
* ListNode next;
51+
* ListNode(int x) { val = x; }
52+
* }
53+
*/
54+
class Solution {
55+
public ListNode removeNthFromEnd(ListNode head, int n) {
56+
ListNode pre = new ListNode(0);
57+
pre.next = head;
58+
ListNode start = pre, end = pre;
59+
while(n != 0) {
60+
start = start.next;
61+
n--;
62+
}
63+
while(start.next != null) {
64+
start = start.next;
65+
end = end.next;
66+
}
67+
end.next = end.next.next;
68+
return pre.next;
69+
}
70+
}
71+
```
72+
73+
### 画解
74+
75+
![fr&lt;x&gt;ame_00001.png](https://i.loli.net/2019/06/08/5cfb590548c8068296.png)
76+
![fr&lt;x&gt;ame_00002.png](https://i.loli.net/2019/06/08/5cfb59056322550694.png)
77+
![fr&lt;x&gt;ame_00003.png](https://i.loli.net/2019/06/08/5cfb590548d3412898.png)
78+
![fr&lt;x&gt;ame_00004.png](https://i.loli.net/2019/06/08/5cfb5cb3942f072681.png)
79+
![fr&lt;x&gt;ame_00005.png](https://i.loli.net/2019/06/08/5cfb590acf6ef19427.png)
80+
![fr&lt;x&gt;ame_00006.png](https://i.loli.net/2019/06/08/5cfb5907c207755226.png)
81+
![fr&lt;x&gt;ame_00007.png](https://i.loli.net/2019/06/08/5cfb5908005cc43738.png)
82+
![fr&lt;x&gt;ame_00008.png](https://i.loli.net/2019/06/08/5cfb590b01fc369826.png)
83+
84+
85+
<span style="display:block;text-align:center;">点击「<strong>阅读原文</strong>」在PC端评论打卡</span>
86+
<span style="display:block;text-align:center;">后台回复「<strong>算法</strong>」,加入天天算法群</span>
87+
<span style="display:block;text-align:center;">觉得算法直击灵魂,欢迎点击<strong>在看</strong>和<strong>转发</strong></span>
88+
89+
![](https://i.loli.net/2019/05/20/5ce23b33cc01d73486.gif)

docs/algorithm/35.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# 画解算法:35. 搜索插入位置
2+
3+
## 题目链接
4+
5+
https://leetcode-cn.com/problems/search-insert-position/
6+
7+
## 题目描述
8+
9+
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
10+
11+
你可以假设数组中无重复元素。
12+
13+
示例 1:
14+
15+
```bash
16+
输入: [1,3,5,6], 5
17+
输出: 2
18+
```
19+
20+
示例 2:
21+
22+
```bash
23+
输入: [1,3,5,6], 2
24+
输出: 1
25+
```
26+
27+
示例 3:
28+
29+
```bash
30+
输入: [1,3,5,6], 7
31+
输出: 4
32+
```
33+
34+
示例 4:
35+
36+
```bash
37+
输入: [1,3,5,6], 0
38+
输出: 0
39+
```
40+
41+
## 解题方案
42+
43+
### 思路
44+
45+
- 标签:二分查找
46+
- 如果该题目暴力解决的话需要O(n)的时间复杂度,但是如果二分的话则可以降低到O(logn)的时间复杂度
47+
- 整体思路和普通的二分查找几乎没有区别,先设定左侧下标`left`和右侧下标`right`,再计算中间下标`mid`
48+
- 每次根据`nums[mid]``target`之间的大小进行判断,相等则直接返回下标,`nums[mid]<target`则left右移,`nums[mid]>target`则right左移
49+
- 查找结束如果没有相等值则返回left,该值为插入位置
50+
- 时间复杂度:O(logn)
51+
52+
二分查找的思路不难理解,但是边界条件容易出错,比如**循环结束条件中left和right的关系,更新left和right位置时要不要加1减1**
53+
54+
下面给出两个可以直接套用的模板,记住就好了,免除边界条件出错。
55+
56+
```java
57+
class Solution {
58+
public int searchInsert(int[] nums, int target) {
59+
int left = 0, right = nums.length - 1; // 注意
60+
while(left <= right) { // 注意
61+
int mid = (left + right) / 2; // 注意
62+
if(nums[mid] == target) { // 注意
63+
// 相关逻辑
64+
} else if(nums[mid] < target) {
65+
left = mid + 1; // 注意
66+
} else {
67+
right = mid - 1; // 注意
68+
}
69+
}
70+
// 相关返回值
71+
return 0;
72+
}
73+
}
74+
```
75+
76+
77+
78+
```java
79+
class Solution {
80+
public int searchInsert(int[] nums, int target) {
81+
int left = 0, right = nums.length; // 注意
82+
while(left < right) { // 注意
83+
int mid = (left + right) / 2; // 注意
84+
if(nums[mid] == target) {
85+
// 相关逻辑
86+
} else if(nums[mid] < target) {
87+
left = mid + 1; // 注意
88+
} else {
89+
right = mid; // 注意
90+
}
91+
}
92+
// 相关返回值
93+
return 0;
94+
}
95+
}
96+
```
97+
98+
### 代码
99+
100+
```java
101+
class Solution {
102+
public int searchInsert(int[] nums, int target) {
103+
int left = 0, right = nums.length - 1;
104+
while(left <= right) {
105+
int mid = (left + right) / 2;
106+
if(nums[mid] == target) {
107+
return mid;
108+
} else if(nums[mid] < target) {
109+
left = mid + 1;
110+
} else {
111+
right = mid - 1;
112+
}
113+
}
114+
return left;
115+
}
116+
}
117+
```
118+
119+
### 画解
120+
121+
![fr&lt;x&gt;ame_00001.png](https://i.loli.net/2019/06/10/5cfdaa6db556988046.png)
122+
![fr&lt;x&gt;ame_00002.png](https://i.loli.net/2019/06/10/5cfdaa6db2b2a58383.png)
123+
![fr&lt;x&gt;ame_00003.png](https://i.loli.net/2019/06/10/5cfdaa6d4400865526.png)
124+
![fr&lt;x&gt;ame_00004.png](https://i.loli.net/2019/06/10/5cfdaa6d8405114145.png)
125+
![fr&lt;x&gt;ame_00005.png](https://i.loli.net/2019/06/10/5cfdaa6d3f50a11555.png)
126+
![fr&lt;x&gt;ame_00006.png](https://i.loli.net/2019/06/10/5cfdaa6db660258479.png)
127+
128+
129+
<span style="display:block;text-align:center;">点击「<strong>阅读原文</strong>」在PC端评论打卡</span>
130+
<span style="display:block;text-align:center;">后台回复「<strong>算法</strong>」,加入天天算法群</span>
131+
<span style="display:block;text-align:center;">觉得算法直击灵魂,欢迎点击<strong>在看</strong>和<strong>转发</strong></span>
132+
133+
![](https://i.loli.net/2019/05/20/5ce23b33cc01d73486.gif)

docs/algorithm/record.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# 题目记录
1+
# 题目打卡记录
22

33
## 2019.05
44

55
|周日|周一|周二|周三|周四|周五|周六|
66
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
77
||||||[884](https://mp.weixin.qq.com/s/VOXoMQg57gdUmwo4553Uag)|[216](https://mp.weixin.qq.com/s/XDpewlE0OUE1DYYE-XdoEg)|
8-
[112](https://mp.weixin.qq.com/s/_NdAjbWlYDV7z7LvMpuyDA)|[938](https://mp.weixin.qq.com/s/ni6l_a1QMCi2sKuZDNoe4Q)|[96](https://mp.weixin.qq.com/s/DEdiaun-RVwRj-9zx9brjw)|[101](https://mp.weixin.qq.com/s/9xiQ8KmTLk_gxf7lsie9dA)|[162](https://mp.weixin.qq.com/s/5HQ5zVPYdUAE4eVJBFKIoQ)|[674](https://mp.weixin.qq.com/s/jSUM4rmYXatKfrjACEdwvw)|
9-
|[704](https://mp.weixin.qq.com/s/mnfLuH1nU8ghShgOhw-KBg)|[77](https://mp.weixin.qq.com/s/VOV5wMsfrLW21ZkqXSfeeA)|||[520](https://mp.weixin.qq.com/s/YJTFk4xEOM9cDM-GQq_EIQ)|[242](https://mp.weixin.qq.com/s/nvxS4p3lXvAL21dDbJo8QA)|[1](https://mp.weixin.qq.com/s/jC4BsJ5r6QixdnwTIchTQw)|
8+
[112](https://mp.weixin.qq.com/s/_NdAjbWlYDV7z7LvMpuyDA)|[938](https://mp.weixin.qq.com/s/ni6l_a1QMCi2sKuZDNoe4Q)|[96](https://mp.weixin.qq.com/s/DEdiaun-RVwRj-9zx9brjw)|[101](https://mp.weixin.qq.com/s/9xiQ8KmTLk_gxf7lsie9dA)|[162](https://mp.weixin.qq.com/s/5HQ5zVPYdUAE4eVJBFKIoQ)|[674](https://mp.weixin.qq.com/s/jSUM4rmYXatKfrjACEdwvw)|92|
9+
|[704](https://mp.weixin.qq.com/s/mnfLuH1nU8ghShgOhw-KBg)|[77](https://mp.weixin.qq.com/s/VOV5wMsfrLW21ZkqXSfeeA)|[1](https://mp.weixin.qq.com/s/jC4BsJ5r6QixdnwTIchTQw)|260|[520](https://mp.weixin.qq.com/s/YJTFk4xEOM9cDM-GQq_EIQ)|[242](https://mp.weixin.qq.com/s/nvxS4p3lXvAL21dDbJo8QA)||
1010

1111

1212
## 2019.06

0 commit comments

Comments
 (0)