Skip to content

Commit 8da0a4c

Browse files
committed
feat: leetcode 刷题
1 parent c104f2b commit 8da0a4c

Some content is hidden

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

52 files changed

+1664
-1427
lines changed

README.md

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@
8181

8282
| 题目 | 难度 | 掌握度 |
8383
| ------------------------------------------------------------------------------ | ---- | ------ |
84-
| [206. 反转链表](https://leetcode.cn/problems/reverse-linked-list/) | 💚 | ✔️ |
85-
| [92. 反转链表 II](https://leetcode.cn/problems/reverse-linked-list-ii/) | 💛 ||
86-
| [25. K 个一组翻转链表](https://leetcode.cn/problems/reverse-nodes-in-k-group/) | ❤️ ||
84+
| [61. 旋转链表](https://leetcode.cn/problems/rotate-list/) | 💛 | ✔️ |
85+
| [206. 反转链表](https://leetcode.cn/problems/reverse-linked-list/) | 💚 | ✔️ |
86+
| [92. 反转链表 II](https://leetcode.cn/problems/reverse-linked-list-ii/) | 💛 ||
87+
| [25. K 个一组翻转链表](https://leetcode.cn/problems/reverse-nodes-in-k-group/) | ❤️ ||
8788

8889
#### 回文链表
8990

@@ -95,59 +96,53 @@
9596

9697
#### 双指针技巧
9798

98-
| 题目 | 掌握度 |
99-
| ------------------------------------------------------------------------------------------------------ | ------ | --- |
100-
| [26. 删除有序数组中的重复项](https://leetcode.cn/problems/remove-duplicates-from-sorted-array/) | ✔️ |
101-
| [27. 移除元素](https://leetcode.cn/problems/remove-element/) | ✔️ |
102-
| [283. 移动零](https://leetcode.cn/problems/move-zeroes/) | ✔️ |
103-
| [704. 二分查找](https://leetcode.cn/problems/binary-search/) | ✔️ |
104-
| [1. 两数之和](https://leetcode.cn/problems/two-sum/) | ✔️ |
105-
| [167. 两数之和 II - 输入有序数组](https://leetcode.cn/problems/two-sum-ii-input-array-is-sorted/) | ✔️ |
106-
| [LCR 179. 查找总价格为目标值的两个商品](https://leetcode.cn/problems/he-wei-sde-liang-ge-shu-zi-lcof/) | ✔️ |
107-
| [LCR 006. 两数之和 II - 输入有序数组](https://leetcode.cn/problems/kLl5u1/) | ✔️ |
108-
| [344. 反转字符串](https://leetcode.cn/problems/reverse-string/) | ✔️ |
109-
| [5. 最长回文子串](https://leetcode.cn/problems/longest-palindromic-substring/) ||
110-
| [378. 有序矩阵中第 K 小的元素](https://leetcode.cn/problems/kth-smallest-element-in-a-sorted-matrix/) | 💛 ||
111-
| [373. 查找和最小的 K 对数字](https://leetcode.cn/problems/find-k-pairs-with-smallest-sums/) | 💛 ||
112-
113-
#### 数组双指针经典习题
114-
115-
| 题目 | 掌握度 |
116-
| ----------------------------------------------------------------------------------------------------- | ------ |
117-
| [80. 删除有序数组中的重复项 II](https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/) | ✔️ |
118-
| [125. 验证回文串](https://leetcode.cn/problems/valid-palindrome/) | ✔️ |
119-
| [75. 颜色分类](https://leetcode.cn/problems/sort-colors/) | ✔️ |
120-
| [88. 合并两个有序数组](https://leetcode.cn/problems/merge-sorted-array/) ||
121-
| [977. 有序数组的平方](https://leetcode.cn/problems/squares-of-a-sorted-array/) | ✔️ |
122-
| [1329. 将矩阵按对角线排序](https://leetcode.cn/problems/sort-the-matrix-diagonally/) ||
123-
| [1260. 二维网格迁移](https://leetcode.cn/problems/shift-2d-grid/) ||
124-
| [867. 转置矩阵](https://leetcode.cn/problems/transpose-matrix/) | ✔️ |
125-
| [14. 最长公共前缀](https://leetcode.cn/problems/longest-common-prefix/) | ✔️ |
126-
127-
#### 二维数组的花式遍历技巧
128-
129-
| 题目 | 掌握度 |
130-
| ----------------------------------------------------------------- | ------ |
131-
| [48. 旋转图像](https://leetcode.cn/problems/rotate-image/) ||
132-
| [54. 螺旋矩阵](https://leetcode.cn/problems/spiral-matrix/) ||
133-
| [59. 螺旋矩阵 II](https://leetcode.cn/problems/spiral-matrix-ii/) ||
99+
| 题目 | 难度 | 掌握度 |
100+
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---- | ------ |
101+
| [27. 移除元素](https://leetcode.cn/problems/remove-element/) | 💚 | ✔️ |
102+
| [283. 移动零](https://leetcode.cn/problems/move-zeroes/) | 💚 | ✔️ |
103+
| [LCR 179. 查找总价格为目标值的两个商品](https://leetcode.cn/problems/he-wei-sde-liang-ge-shu-zi-lcof/) | 💚 | ✔️ |
104+
| [1. 两数之和](https://leetcode.cn/problems/two-sum/) | 💚 | ✔️ |
105+
| [167. 两数之和 II - 输入有序数组](https://leetcode.cn/problems/two-sum-ii-input-array-is-sorted/)<br/>[LCR 006. 两数之和 II - 输入有序数组](https://leetcode.cn/problems/kLl5u1/) | 💛 | ✔️ |
106+
| [26. 删除有序数组中的重复项](https://leetcode.cn/problems/remove-duplicates-from-sorted-array/) | 💚 | ✔️ |
107+
| [80. 删除有序数组中的重复项 II](https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/) | 💛 | ✔️ |
108+
| [344. 反转字符串](https://leetcode.cn/problems/reverse-string/) | 💚 | ✔️ |
109+
| [125. 验证回文串](https://leetcode.cn/problems/valid-palindrome/) | 💚 | ✔️ |
110+
| [5. 最长回文子串](https://leetcode.cn/problems/longest-palindromic-substring/) | 💛 ||
111+
| [75. 颜色分类](https://leetcode.cn/problems/sort-colors/) | 💛 | ✔️ |
112+
| [88. 合并两个有序数组](https://leetcode.cn/problems/merge-sorted-array/) | 💚 ||
113+
| [977. 有序数组的平方](https://leetcode.cn/problems/squares-of-a-sorted-array/) | 💚 | ✔️ |
114+
| [1329. 将矩阵按对角线排序](https://leetcode.cn/problems/sort-the-matrix-diagonally/) | 💛 ||
115+
| [1260. 二维网格迁移](https://leetcode.cn/problems/shift-2d-grid/) | 💚 ||
116+
| [867. 转置矩阵](https://leetcode.cn/problems/transpose-matrix/) | 💚 | ✔️ |
117+
| [14. 最长公共前缀](https://leetcode.cn/problems/longest-common-prefix/) | 💚 | ✔️ |
118+
| [378. 有序矩阵中第 K 小的元素](https://leetcode.cn/problems/kth-smallest-element-in-a-sorted-matrix/) | 💛 | |
119+
| [373. 查找和最小的 K 对数字](https://leetcode.cn/problems/find-k-pairs-with-smallest-sums/) | 💛 | |
120+
121+
#### 二维数组遍历
122+
123+
| 题目 | 难度 | 掌握度 |
124+
| ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---- | ------ |
125+
| [151. 反转字符串中的单词](https://leetcode.cn/problems/reverse-words-in-a-string/) | 💛 ||
126+
| [48. 旋转图像](https://leetcode.cn/problems/rotate-image/) | 💛 ||
127+
| [54. 螺旋矩阵](https://leetcode.cn/problems/spiral-matrix/)<br/>[LCR 146. 螺旋遍历二维数组](https://leetcode.cn/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/) | 💛 ||
128+
| [59. 螺旋矩阵 II](https://leetcode.cn/problems/spiral-matrix-ii/) | 💛 ||
134129

135130
#### 滑动窗口算法
136131

137-
| 题目 | 掌握度 |
138-
| -------------------------------------------------------------------------------------------------------------------------- | ------ |
139-
| [3. 无重复字符的最长子串](https://leetcode.cn/problems/longest-substring-without-repeating-characters/) | ✔️ |
140-
| [438. 找到字符串中所有字母异位词](https://leetcode.cn/problems/find-all-anagrams-in-a-string/) ||
141-
| [567. 字符串的排列](https://leetcode.cn/problems/permutation-in-string/) ||
142-
| [76. 最小覆盖子串](https://leetcode.cn/problems/minimum-window-substring/) ||
143-
| [1658. 将 x 减到 0 的最小操作数](https://leetcode.cn/problems/minimum-operations-to-reduce-x-to-zero/) ||
144-
| [713. 乘积小于 K 的子数组](https://leetcode.cn/problems/subarray-product-less-than-k/) ||
145-
| [1004. 最大连续 1 的个数 III](https://leetcode.cn/problems/max-consecutive-ones-iii/) | ✔️ |
146-
| [424. 替换后的最长重复字符](https://leetcode.cn/problems/longest-repeating-character-replacement/) ||
147-
| [219. 存在重复元素 II](https://leetcode.cn/problems/contains-duplicate-ii/) ||
148-
| [220. 存在重复元素 III](https://leetcode.cn/problems/contains-duplicate-iii/) ||
149-
| [209. 长度最小的子数组](https://leetcode.cn/problems/minimum-size-subarray-sum/) ||
150-
| [395. 至少有 K 个重复字符的最长子串](https://leetcode.cn/problems/longest-substring-with-at-least-k-repeating-characters/) ||
132+
| 题目 | 难度 | 掌握度 |
133+
| -------------------------------------------------------------------------------------------------------------------------- | ------ | ------ |
134+
| [3. 无重复字符的最长子串](https://leetcode.cn/problems/longest-substring-without-repeating-characters/) | | |
135+
| [438. 找到字符串中所有字母异位词](https://leetcode.cn/problems/find-all-anagrams-in-a-string/) | | |
136+
| [567. 字符串的排列](https://leetcode.cn/problems/permutation-in-string/) | | |
137+
| [76. 最小覆盖子串](https://leetcode.cn/problems/minimum-window-substring/) | | |
138+
| [1658. 将 x 减到 0 的最小操作数](https://leetcode.cn/problems/minimum-operations-to-reduce-x-to-zero/) | | |
139+
| [713. 乘积小于 K 的子数组](https://leetcode.cn/problems/subarray-product-less-than-k/) | | |
140+
| [1004. 最大连续 1 的个数 III](https://leetcode.cn/problems/max-consecutive-ones-iii/) | | ✔️ |
141+
| [424. 替换后的最长重复字符](https://leetcode.cn/problems/longest-repeating-character-replacement/) | | |
142+
| [219. 存在重复元素 II](https://leetcode.cn/problems/contains-duplicate-ii/) | | |
143+
| [220. 存在重复元素 III](https://leetcode.cn/problems/contains-duplicate-iii/) | | |
144+
| [209. 长度最小的子数组](https://leetcode.cn/problems/minimum-size-subarray-sum/) | | |
145+
| [395. 至少有 K 个重复字符的最长子串](https://leetcode.cn/problems/longest-substring-with-at-least-k-repeating-characters/) | | |
151146

152147
#### 二分查找算法
153148

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package io.github.dunwu.algorithm.array.matrix;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
5+
/**
6+
* <a href="https://leetcode.cn/problems/reverse-words-in-a-string/">151. 反转字符串中的单词</a>
7+
*
8+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
9+
* @since 2020-06-05
10+
*/
11+
public class 反转字符串中的单词 {
12+
13+
public static void main(String[] args) {
14+
Solution s = new Solution();
15+
Assertions.assertEquals("blue is sky the", s.reverseWords("the sky is blue"));
16+
Assertions.assertEquals("world hello", s.reverseWords(" hello world "));
17+
Assertions.assertEquals("example good a", s.reverseWords("a good example"));
18+
19+
Solution2 s2 = new Solution2();
20+
Assertions.assertEquals("blue is sky the", s2.reverseWords("the sky is blue"));
21+
Assertions.assertEquals("world hello", s2.reverseWords(" hello world "));
22+
Assertions.assertEquals("example good a", s2.reverseWords("a good example"));
23+
}
24+
25+
// 利用库函数
26+
static class Solution {
27+
28+
public String reverseWords(String s) {
29+
String[] strs = s.split(" ");
30+
StringBuilder sb = new StringBuilder();
31+
for (int i = strs.length - 1; i >= 0; i--) {
32+
if (strs[i].equals("")) {
33+
continue;
34+
}
35+
sb.append(strs[i]).append(" ");
36+
}
37+
if (sb.lastIndexOf(" ") == sb.length() - 1) {
38+
return sb.substring(0, sb.length() - 1);
39+
}
40+
return sb.toString();
41+
}
42+
43+
}
44+
45+
// 双指针
46+
static class Solution2 {
47+
48+
public String reverseWords(String s) {
49+
// 删除首尾空格
50+
s = s.trim();
51+
int i = s.length() - 1, j = i;
52+
StringBuilder res = new StringBuilder();
53+
while (i >= 0) {
54+
// 搜索首个空格
55+
while (i >= 0 && s.charAt(i) != ' ') { i--; }
56+
// 添加单词
57+
res.append(s.substring(i + 1, j + 1)).append(" ");
58+
// 跳过单词间空格
59+
while (i >= 0 && s.charAt(i) == ' ') { i--; }
60+
// j 指向下个单词的尾字符
61+
j = i;
62+
}
63+
// 转化为字符串并返回
64+
return res.toString().trim();
65+
}
66+
67+
}
68+
69+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.github.dunwu.algorithm.array.matrix;
2+
3+
import cn.hutool.core.util.ArrayUtil;
4+
import org.junit.jupiter.api.Assertions;
5+
6+
/**
7+
* <a href="https://leetcode.cn/problems/rotate-image/">LCR 006. 两数之和 II - 输入有序数组</a>
8+
*
9+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
10+
* @since 2025-08-06
11+
*/
12+
public class 旋转图像 {
13+
14+
public static void main(String[] args) {
15+
Solution s = new Solution();
16+
int[][] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
17+
s.rotate(matrix);
18+
int[][] expect = { { 7, 4, 1 }, { 8, 5, 2 }, { 9, 6, 3 } };
19+
Assertions.assertTrue(ArrayUtil.equals(expect, matrix));
20+
21+
int[][] matrix2 = { { 5, 1, 9, 11 }, { 2, 4, 8, 10 }, { 13, 3, 6, 7 }, { 15, 14, 12, 16 } };
22+
s.rotate(matrix2);
23+
int[][] expect2 = { { 15, 13, 2, 5 }, { 14, 3, 4, 1 }, { 12, 6, 8, 9 }, { 16, 7, 10, 11 } };
24+
Assertions.assertTrue(ArrayUtil.equals(expect2, matrix2));
25+
}
26+
27+
static class Solution {
28+
29+
public void rotate(int[][] matrix) {
30+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { return; }
31+
int n = matrix.length;
32+
// 沿对角线置换
33+
for (int i = 0; i < n; i++) {
34+
for (int j = 0; j < i; j++) {
35+
int temp = matrix[i][j];
36+
matrix[i][j] = matrix[j][i];
37+
matrix[j][i] = temp;
38+
}
39+
}
40+
41+
for (int i = 0; i < n; i++) {
42+
int left = 0, right = n - 1;
43+
while (left < right) {
44+
int temp = matrix[i][left];
45+
matrix[i][left] = matrix[i][right];
46+
matrix[i][right] = temp;
47+
left++;
48+
right--;
49+
}
50+
}
51+
}
52+
53+
}
54+
55+
}

0 commit comments

Comments
 (0)