Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 98432fb

Browse files
committed
clean code
1 parent b7c120f commit 98432fb

File tree

24 files changed

+215
-171
lines changed

24 files changed

+215
-171
lines changed

‎leetcode/leetcode-01/src/main/java/Solution64.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ public int minPathSum(int[][] grid) {
4040
定义 dp[i][j] 为 i x j 矩阵中,路径上的数字总和为最小的值。
4141
相似题目: 剑指 Offer 47. 礼物的最大价值
4242
https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/
43+
3603. 交替方向的最小路径代价 II
44+
https://leetcode.cn/problems/minimum-cost-path-with-alternating-directions-ii/description/
4345
*/

‎leetcode/leetcode-02/src/main/java/Solution135.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
public class Solution135 {
22
public int candy(int[] ratings) {
3-
int len = ratings.length;
3+
int n = ratings.length;
44

5-
// 左往右
6-
int[] left = new int[len];
7-
for (int i = 0; i < len; i++) {
8-
if (i > 0 && ratings[i] > ratings[i - 1]) {
5+
int[] left = new int[n];
6+
for (int i = 0; i < n; i++) {
7+
if (i - 1 >= 0 && ratings[i] > ratings[i - 1]) {
98
left[i] = left[i - 1] + 1;
109
} else {
1110
left[i] = 1;
1211
}
1312
}
1413

15-
// 右往左
16-
int[] right = new int[len];
17-
for (int i = len - 1; i >= 0; i--) {
18-
if (i + 1 < len && ratings[i] > ratings[i + 1]) {
14+
int[] right = new int[n];
15+
for (int i = n - 1; i >= 0; i--) {
16+
if (i + 1 < n && ratings[i] > ratings[i + 1]) {
1917
right[i] = right[i + 1] + 1;
2018
} else {
2119
right[i] = 1;
2220
}
2321
}
2422

25-
int sum = 0;
26-
for (int i = 0; i < len; i++) {
27-
sum += Math.max(left[i], right[i]);
23+
int ans = 0;
24+
for (int i = 0; i < n; i++) {
25+
ans += Math.max(left[i], right[i]);
2826
}
29-
return sum;
27+
return ans;
3028
}
3129
}
3230
/*

‎leetcode/leetcode-02/src/main/java/Solution188.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,6 @@ private int dfs(int i, int j, int hold) {
4545
0 <= prices[i] <= 1000
4646
4747
状态机 DP
48+
相似题目: 3573. 买卖股票的最佳时机 V
49+
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-v/description/
4850
*/

‎leetcode/leetcode-06/src/main/java/Solution518.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ public int change(int amount, int[] coins) {
2727
完全背包 求方案数。
2828
相似题目: 322. 零钱兑换
2929
https://leetcode.cn/problems/coin-change/
30+
3592. 硬币面值还原
31+
https://leetcode.cn/problems/inverse-coin-change/description/
3032
*/

‎leetcode/leetcode-08/src/main/java/Solution780.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ public boolean reachingPoints(int sx, int sy, int tx, int ty) {
2424
即 点 (x, y) 恢复到 (x, y-x) 或者 (x-y, y)。由于点为正整数,因此只有一种逆向可能。
2525
时间复杂度 O(logn)
2626
空间复杂度 O(1)
27+
相似题目: 3609. 到达目标点的最小移动次数
28+
https://leetcode.cn/problems/minimum-moves-to-reach-target-in-grid/description/
2729
*/

‎leetcode/leetcode-10/src/main/java/Solution909.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import java.util.LinkedList;
1+
import java.util.ArrayDeque;
22
import java.util.Queue;
33

44
public class Solution909 {
55
public int snakesAndLadders(int[][] board) {
66
int n = board.length;
7-
Queue<Integer> queue = new LinkedList<>();
8-
queue.add(1);
9-
boolean[] visited = new boolean[n * n + 1];
10-
visited[1] = true;
7+
Queue<Integer> q = new ArrayDeque<>();
8+
q.add(1);
9+
boolean[] vis = new boolean[n * n + 1];
10+
vis[1] = true;
1111
int step = 0;
12-
while (!queue.isEmpty()) {
13-
int size = queue.size();
12+
while (!q.isEmpty()) {
13+
int size = q.size();
1414
for (int i = 0; i < size; i++) {
15-
int cur = queue.remove();
15+
int cur = q.remove();
1616
if (cur == n * n) {
1717
return step;
1818
}
@@ -26,9 +26,9 @@ public int snakesAndLadders(int[][] board) {
2626
if (board[rc[0]][rc[1]] != -1) {
2727
next = board[rc[0]][rc[1]];
2828
}
29-
if (!visited[next]) {
30-
visited[next] = true;
31-
queue.add(next);
29+
if (!vis[next]) {
30+
vis[next] = true;
31+
q.add(next);
3232
}
3333
}
3434
}

‎leetcode/leetcode-11/src/main/java/Solution1036.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import java.util.ArrayDeque;
12
import java.util.Arrays;
23
import java.util.HashSet;
34
import java.util.LinkedList;
45
import java.util.Queue;
56
import java.util.Set;
67

78
public class Solution1036 {
9+
private static final int[][] DIRECTIONS = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
810
private static final int M = 1000000;
911
private static final int N = 1000000;
1012

@@ -21,33 +23,32 @@ public boolean isEscapePossible(int[][] blocked, int[] source, int[] target) {
2123
}
2224

2325
private boolean bfs(int[] source, int[] target, Set<String> blockedSet) {
24-
int[][] direction = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
2526
// BFS
26-
Queue<int[]> queue = new LinkedList<>();
27-
Set<String> visitedSet = new HashSet<>();
28-
queue.add(source);
29-
visitedSet.add(source[0] + ":" + source[1]);
27+
Queue<int[]> q = new ArrayDeque<>();
28+
Set<String> vis = new HashSet<>();
29+
q.add(source);
30+
vis.add(source[0] + ":" + source[1]);
3031

31-
// blockedS 可以围住的最大个数
32-
int len = blockedSet.size();
33-
int max = len * (len - 1) / 2;
32+
// blockedSet 可以围住的最大个数
33+
int k = blockedSet.size();
34+
int max = k * (k - 1) / 2;
3435
int cnt = 0;
35-
while (!queue.isEmpty()) {
36-
int size = queue.size();
36+
while (!q.isEmpty()) {
37+
int size = q.size();
3738
cnt += size;
3839
for (int i = 0; i < size; i++) {
39-
int[] cur = queue.remove();
40+
int[] cur = q.remove();
4041
// 到达目标方格
4142
if (Arrays.equals(cur, target)) {
4243
return true;
4344
}
44-
for (int[] dir : direction) {
45-
int nextM = cur[0] + dir[0];
46-
int nextN = cur[1] + dir[1];
47-
if (nextM >= 0 && nextM < M && nextN >= 0 && nextN < N && !visitedSet.contains(nextM + ":" + nextN)
48-
&& !blockedSet.contains(nextM + ":" + nextN)) {
49-
visitedSet.add(nextM + ":" + nextN);
50-
queue.add(new int[]{nextM, nextN});
45+
for (int[] d : DIRECTIONS) {
46+
int nx = cur[0] + d[0];
47+
int ny = cur[1] + d[1];
48+
if (nx >= 0 && nx < M && ny >= 0 && ny < N && !vis.contains(nx + ":" + ny)
49+
&& !blockedSet.contains(nx + ":" + ny)) {
50+
vis.add(nx + ":" + ny);
51+
q.add(new int[]{nx, ny});
5152
}
5253
}
5354
}

‎leetcode/leetcode-11/src/main/java/Solution1061.java

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,43 @@
11
public class Solution1061 {
22
public String smallestEquivalentString(String s1, String s2, String baseStr) {
3-
int len = s1.length();
3+
int n = s1.length();
44

55
DSU dsu = new DSU(26);
6-
for (int i = 0; i < len; i++) {
6+
for (int i = 0; i < n; i++) {
77
int idx1 = s1.charAt(i) - 'a';
88
int idx2 = s2.charAt(i) - 'a';
99
dsu.union(idx1, idx2);
1010
}
1111

12-
StringBuilder stringBuilder = new StringBuilder();
12+
StringBuilder ans = new StringBuilder();
1313
for (char ch : baseStr.toCharArray()) {
1414
int idx = dsu.find(ch - 'a');
15-
stringBuilder.append((char) (idx + 'a'));
15+
ans.append((char) (idx + 'a'));
1616
}
17-
return stringBuilder.toString();
17+
return ans.toString();
1818
}
1919

20-
private static class DSU {
21-
// 父节点数组/祖先数组
20+
static class DSU {
2221
int[] fa;
2322

24-
// 初始化
2523
public DSU(int n) {
2624
fa = new int[n];
2725
for (int i = 0; i < n; i++) {
2826
fa[i] = i;
2927
}
3028
}
3129

32-
// 查找
33-
int find(int x) {
34-
// 路径压缩
35-
if (x != fa[x]) {
36-
fa[x] = find(fa[x]);
37-
}
38-
return fa[x];
30+
int find(int x) { // 查找
31+
return x == fa[x] ? fa[x] : (fa[x] = find(fa[x]));
3932
}
4033

41-
// 合并
42-
void union(int p, int q) {
43-
int rootP = find(p);
44-
int rootQ = find(q);
45-
if (rootP == rootQ) {
46-
return;
47-
}
34+
void union(int p, int q) { // 合并
35+
p = find(p);
36+
q = find(q);
37+
if (p == q) return;
4838
// 不按 rank,连接到字典序小的点
49-
if (rootP < rootQ) {
50-
fa[rootQ] = rootP;
51-
} else {
52-
fa[rootP] = rootQ;
53-
}
39+
if (p < q) fa[q] = p;
40+
else fa[p] = q;
5441
}
5542
}
5643
}

‎leetcode/leetcode-17/src/main/java/Solution1638.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ public int countSubstrings(String s, String t) {
1717
}
1818
return ans;
1919
}
20+
21+
public int countSubstrings2(String S, String T) {
22+
char[] s = S.toCharArray();
23+
char[] t = T.toCharArray();
24+
int n = s.length;
25+
int m = t.length;
26+
int ans = 0;
27+
for (int d = 1 - m; d < n; ++d) { // d=i-j, j=i-d
28+
int i = Math.max(d, 0);
29+
for (int k0 = i - 1, k1 = k0; i < n && i - d < m; ++i) {
30+
if (s[i] != t[i - d]) {
31+
k0 = k1; // 上上一个不同
32+
k1 = i; // 上一个不同
33+
}
34+
ans += k1 - k0;
35+
}
36+
}
37+
return ans;
38+
}
2039
}
2140
/*
2241
1638. 统计只差一个字符的子串数目
@@ -32,4 +51,6 @@ public int countSubstrings(String s, String t) {
3251
3352
枚举。
3453
时间复杂度 O(mn * min(m,n))。
54+
【图解】非暴力 O(nm) 算法
55+
https://leetcode.cn/problems/count-substrings-that-differ-by-one-character/solutions/2192600/tu-jie-fei-bao-li-onm-suan-fa-pythonjava-k5og/
3556
*/

‎leetcode/leetcode-17/src/test/java/Solution1638Tests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public void example1() {
1010
String t = "baba";
1111
int expected = 6;
1212
Assertions.assertEquals(expected, solution1638.countSubstrings(s, t));
13+
Assertions.assertEquals(expected, solution1638.countSubstrings2(s, t));
1314
}
1415

1516
@Test
@@ -18,6 +19,7 @@ public void example2() {
1819
String t = "bb";
1920
int expected = 3;
2021
Assertions.assertEquals(expected, solution1638.countSubstrings(s, t));
22+
Assertions.assertEquals(expected, solution1638.countSubstrings2(s, t));
2123
}
2224

2325
@Test
@@ -26,6 +28,7 @@ public void example3() {
2628
String t = "a";
2729
int expected = 0;
2830
Assertions.assertEquals(expected, solution1638.countSubstrings(s, t));
31+
Assertions.assertEquals(expected, solution1638.countSubstrings2(s, t));
2932
}
3033

3134
@Test
@@ -34,5 +37,6 @@ public void example4() {
3437
String t = "bbc";
3538
int expected = 10;
3639
Assertions.assertEquals(expected, solution1638.countSubstrings(s, t));
40+
Assertions.assertEquals(expected, solution1638.countSubstrings2(s, t));
3741
}
3842
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /