Skip to content

Commit a3f32a1

Browse files
committed
auto commit
1 parent a1debab commit a3f32a1

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

notes/Leetcode 题解.md

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Output: True
103103
Explanation: 1 * 1 + 2 * 2 = 5
104104
```
105105

106-
题目描述:判断一个数是否为两个数的平方和,例如 5 = 1<sup>2</sup> + 2<sup>2</sup>
106+
题目描述:判断一个数是否为两个数的平方和。
107107

108108
```java
109109
public boolean judgeSquareSum(int c) {
@@ -130,7 +130,7 @@ public boolean judgeSquareSum(int c) {
130130
Given s = "leetcode", return "leotcede".
131131
```
132132

133-
使用双指针,指向待反转的两个元音字符,一个指针从头向尾遍历,一个指针从尾到头遍历。
133+
使用双指针指向待反转的两个元音字符,一个指针从头向尾遍历,一个指针从尾到头遍历。
134134

135135
```java
136136
private final static HashSet<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
@@ -307,6 +307,8 @@ private boolean isValid(String s, String target) {
307307

308308
[215. Kth Largest Element in an Array (Medium)](https://leetcode.com/problems/kth-largest-element-in-an-array/description/)
309309

310+
题目描述:找到第 k 大的元素。
311+
310312
**排序** :时间复杂度 O(NlogN),空间复杂度 O(1)
311313

312314
```java
@@ -323,7 +325,7 @@ public int findKthLargest(int[] nums, int k) {
323325
PriorityQueue<Integer> pq = new PriorityQueue<>(); // 小顶堆
324326
for (int val : nums) {
325327
pq.add(val);
326-
if (pq.size() > k) // 维护堆的大小为 K
328+
if (pq.size() > k) // 维护堆的大小为 K
327329
pq.poll();
328330
}
329331
return pq.peek();
@@ -555,7 +557,7 @@ Explanation: You don't need to remove any of the intervals since they're already
555557

556558
题目描述:计算让一组区间不重叠所需要移除的区间个数。
557559

558-
计算最多能组成的不重叠区间个数,然后用区间总个数减去不重叠区间的个数。
560+
先计算最多能组成的不重叠区间个数,然后用区间总个数减去不重叠区间的个数。
559561

560562
在每次选择中,区间的结尾最为重要,选择的区间结尾越小,留给后面的区间的空间越大,那么后面能够选择的区间个数也就越大。
561563

@@ -639,7 +641,7 @@ Output:
639641

640642
题目描述:一个学生用两个分量 (h, k) 描述,h 表示身高,k 表示排在前面的有 k 个学生的身高比他高或者和他一样高。
641643

642-
为了在每次插入操作时不影响后续的操作,身高较高的学生应该先做插入操作,否则身高较小的学生原先正确插入第 k 个位置可能会变成第 k+1 个位置。
644+
为了使插入操作不影响后续的操作,身高较高的学生应该先做插入操作,否则身高较小的学生原先正确插入的第 k 个位置可能会变成第 k+1 个位置。
643645

644646
身高降序、k 值升序,然后按排好序的顺序插入队列的第 k 个位置中。
645647

@@ -825,7 +827,7 @@ public int binarySearch(int[] nums, int key) {
825827

826828
**时间复杂度**
827829

828-
二分查找也称为折半查找,每次都能将查找区间减半,这种折半特性的算法时间复杂度都为 O(logN)。
830+
二分查找也称为折半查找,每次都能将查找区间减半,这种折半特性的算法时间复杂度为 O(logN)。
829831

830832
**m 计算**
831833

@@ -961,7 +963,7 @@ public char nextGreatestLetter(char[] letters, char target) {
961963
[540. Single Element in a Sorted Array (Medium)](https://leetcode.com/problems/single-element-in-a-sorted-array/description/)
962964

963965
```html
964-
Input: [1,1,2,3,3,4,4,8,8]
966+
Input: [1, 1, 2, 3, 3, 4, 4, 8, 8]
965967
Output: 2
966968
```
967969

@@ -1132,11 +1134,11 @@ public List<Integer> diffWaysToCompute(String input) {
11321134

11331135
<div align="center"> <img src="../pics//4ff355cf-9a7f-4468-af43-e5b02038facc.jpg"/> </div><br>
11341136

1135-
广度优先搜索的搜索过程有点像一层一层地进行遍历,每层遍历都以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。
1137+
广度优先搜索一层一层地进行遍历,每层遍历都以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。
11361138

11371139
第一层:
11381140

1139-
- 0 -> {6,2,1,5};
1141+
- 0 -> {6,2,1,5}
11401142

11411143
第二层:
11421144

@@ -1150,7 +1152,7 @@ public List<Integer> diffWaysToCompute(String input) {
11501152
- 4 -> {}
11511153
- 3 -> {}
11521154

1153-
可以看到,每一层遍历的节点都与根节点距离相同。设 d<sub>i</sub> 表示第 i 个节点与根节点的距离,推导出一个结论:对于先遍历的节点 i 与后遍历的节点 j,有 d<sub>i</sub><=d<sub>j</sub>。利用这个结论,可以求解最短路径等 **最优解** 问题:第一次遍历到目的节点,其所经过的路径为最短路径。应该注意的是,使用 BFS 只能求解无权图的最短路径。
1155+
每一层遍历的节点都与根节点距离相同。设 d<sub>i</sub> 表示第 i 个节点与根节点的距离,推导出一个结论:对于先遍历的节点 i 与后遍历的节点 j,有 d<sub>i</sub> <= d<sub>j</sub>。利用这个结论,可以求解最短路径等 **最优解** 问题:第一次遍历到目的节点,其所经过的路径为最短路径。应该注意的是,使用 BFS 只能求解无权图的最短路径。
11541156

11551157
在程序实现 BFS 时需要考虑以下问题:
11561158

@@ -1180,19 +1182,17 @@ public int minPathLength(int[][] grids, int tr, int tc) {
11801182
pathLength++;
11811183
while (size-- > 0) {
11821184
Pair<Integer, Integer> cur = queue.poll();
1185+
int cr = cur.getKey(), cc = cur.getValue();
1186+
grids[cr][cc] = 0; // 标记
11831187
for (int[] d : direction) {
1184-
int nr = cur.getKey() + d[0], nc = cur.getValue() + d[1];
1185-
Pair<Integer, Integer> next = new Pair<>(nr, nc);
1186-
if (next.getKey() < 0 || next.getValue() >= m
1187-
|| next.getKey() < 0 || next.getValue() >= n) {
1188-
1188+
int nr = cr + d[0], nc = cc + d[1];
1189+
if (nr < 0 || nr >= m || nc < 0 || nc >= n || grids[nr][nc] == 0) {
11891190
continue;
11901191
}
1191-
grids[next.getKey()][next.getValue()] = 0; // 标记
1192-
if (next.getKey() == tr && next.getValue() == tc) {
1192+
if (nr == tr && nc == tc) {
11931193
return pathLength;
11941194
}
1195-
queue.add(next);
1195+
queue.add(new Pair<>(nr, nc));
11961196
}
11971197
}
11981198
}
@@ -1239,7 +1239,7 @@ public int numSquares(int n) {
12391239
continue;
12401240
}
12411241
marked[next] = true;
1242-
queue.add(cur - s);
1242+
queue.add(next);
12431243
}
12441244
}
12451245
}
@@ -1290,7 +1290,7 @@ Output: 0
12901290
Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.
12911291
```
12921292

1293-
找出一条从 beginWord 到 endWord 的最短路径,每次移动规定为改变一个字符,并且改变之后的字符串必须在 wordList 中。
1293+
题目描述:找出一条从 beginWord 到 endWord 的最短路径,每次移动规定为改变一个字符,并且改变之后的字符串必须在 wordList 中。
12941294

12951295
```java
12961296
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
@@ -1365,7 +1365,7 @@ private int getShortestPath(List<Integer>[] graphic, int start, int end) {
13651365

13661366
广度优先搜索一层一层遍历,每一层得到的所有新节点,要用队列存储起来以备下一层遍历的时候再遍历。
13671367

1368-
而深度优先搜索在得到一个新节点时立马对新节点进行遍历:从节点 0 出发开始遍历,得到到新节点 6 时,立马对新节点 6 进行遍历,得到新节点 4;如此反复以这种方式遍历新节点,直到没有新节点了,此时返回。返回到根节点 0 的情况是,继续对根节点 0 进行遍历,得到新节点 2,然后继续以上步骤。
1368+
而深度优先搜索在得到一个新节点时立即对新节点进行遍历:从节点 0 出发开始遍历,得到到新节点 6 时,立马对新节点 6 进行遍历,得到新节点 4;如此反复以这种方式遍历新节点,直到没有新节点了,此时返回。返回到根节点 0 的情况是,继续对根节点 0 进行遍历,得到新节点 2,然后继续以上步骤。
13691369

13701370
从一个节点出发,使用 DFS 对一个图进行遍历时,能够遍历到的节点都是从初始节点可达的,DFS 常用来求解这种 **可达性** 问题。
13711371

@@ -1479,12 +1479,14 @@ Input:
14791479
[[1,1,0],
14801480
[1,1,0],
14811481
[0,0,1]]
1482+
14821483
Output: 2
1484+
14831485
Explanation:The 0th and 1st students are direct friends, so they are in a friend circle.
14841486
The 2nd student himself is in a friend circle. So return 2.
14851487
```
14861488

1487-
好友关系可以看成是一个无向图,例如第 0 个人与第 1 个人是好友,那么 M[0][1] 和 M[1][0] 的值都为 1。
1489+
题目描述:好友关系可以看成是一个无向图,例如第 0 个人与第 1 个人是好友,那么 M[0][1] 和 M[1][0] 的值都为 1。
14881490

14891491
```java
14901492
private int n;
@@ -1530,7 +1532,7 @@ X X X X
15301532
X O X X
15311533
```
15321534

1533-
使被 'X' 包围的 'O' 转换为 'X'。
1535+
题目描述:使被 'X' 包围的 'O' 转换为 'X'。
15341536

15351537
先填充最外侧,剩下的就是里侧了。
15361538

@@ -1678,7 +1680,6 @@ Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
16781680
```
16791681

16801682
```java
1681-
16821683
private static final String[] KEYS = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
16831684

16841685
public List<String> letterCombinations(String digits) {
@@ -2316,11 +2317,11 @@ private int cubeNum(int i, int j) {
23162317

23172318
一行一行地摆放,在确定一行中的那个皇后应该摆在哪一列时,需要用三个标记数组来确定某一列是否合法,这三个标记数组分别为:列标记数组、45 度对角线标记数组和 135 度对角线标记数组。
23182319

2319-
45 度对角线标记数组的维度为 2 \* n - 1,通过下图可以明确 (r, c) 的位置所在的数组下标为 r + c。
2320+
45 度对角线标记数组的长度为 2 \* n - 1,通过下图可以明确 (r, c) 的位置所在的数组下标为 r + c。
23202321

23212322
<div align="center"> <img src="../pics//85583359-1b45-45f2-9811-4f7bb9a64db7.jpg"/> </div><br>
23222323

2323-
135 度对角线标记数组的维度也是 2 \* n - 1,(r, c) 的位置所在的数组下标为 n - 1 - (r - c)。
2324+
135 度对角线标记数组的长度也是 2 \* n - 1,(r, c) 的位置所在的数组下标为 n - 1 - (r - c)。
23242325

23252326
<div align="center"> <img src="../pics//9e80f75a-b12b-4344-80c8-1f9ccc2d5246.jpg"/> </div><br>
23262327

@@ -2414,9 +2415,9 @@ public int climbStairs(int n) {
24142415

24152416
定义 dp 数组用来存储最大的抢劫量,其中 dp[i] 表示抢到第 i 个住户时的最大抢劫量。
24162417

2417-
由于不能抢劫邻近住户,因此如果抢劫了第 i 个住户那么只能抢劫 i - 2 或者 i - 3 的住户,所以
2418+
由于不能抢劫邻近住户,如果抢劫了第 i -1 个住户,那么就不能再抢劫第 i 个住户,所以
24182419

2419-
<div align="center"><img src="https://latex.codecogs.com/gif.latex?dp[i]=max(dp[i-2],dp[i-3])+nums[i]"/></div> <br>
2420+
<div align="center"><img src="https://latex.codecogs.com/gif.latex?dp[i]=max(dp[i-2]+nums[i],dp[i-1])"/></div> <br>
24202421

24212422
```java
24222423
public int rob(int[] nums) {

0 commit comments

Comments
 (0)