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 9457a6e

Browse files
committed
clean code
1 parent 0934c1c commit 9457a6e

File tree

15 files changed

+219
-217
lines changed

15 files changed

+219
-217
lines changed

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@
33

44
public class Solution128 {
55
public int longestConsecutive(int[] nums) {
6-
Set<Integer> hashSet = new HashSet<>();
7-
for (int num : nums) {
8-
hashSet.add(num);
6+
Set<Integer> set = new HashSet<>();
7+
for (int v : nums) {
8+
set.add(v);
99
}
1010

11-
int max = 0;
12-
for (int num : nums) {
13-
if (!hashSet.contains(num - 1)) {
14-
int cnt = 0;
15-
while (hashSet.contains(num + cnt)) {
16-
cnt++;
17-
}
18-
max = Math.max(max, cnt);
11+
int ans = 0;
12+
for (int v : set) {
13+
if (set.contains(v - 1)) continue;
14+
int cnt = 0;
15+
while (set.contains(v + cnt)) {
16+
cnt++;
1917
}
18+
ans = Math.max(ans, cnt);
2019
}
21-
return max;
20+
return ans;
2221
}
2322
}
2423
/*

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

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,46 @@
33
import java.util.List;
44

55
public class Solution131 {
6-
/**
7-
* 剑指 Offer II 086. 分割回文子字符串
8-
* https://leetcode.cn/problems/M99OJA/
9-
*/
6+
// 剑指 Offer II 086. 分割回文子字符串
7+
// https://leetcode.cn/problems/M99OJA/
108
public String[][] partition(String s) {
11-
List<List<String>> resList = partition131(s);
12-
// List<List<String>> 转 String[][]
13-
int resM = resList.size();
14-
String[][] res = new String[resM][];
15-
for (int i = 0; i < resM; i++) {
16-
int resN = resList.get(i).size();
17-
res[i] = new String[resN];
18-
for (int j = 0; j < resN; j++) {
19-
res[i][j] = resList.get(i).get(j);
20-
}
21-
}
22-
return res;
9+
V1 sol = new V1();
10+
List<List<String>> res = sol.partition(s);
11+
return res.stream().map(row -> row.toArray(String[]::new)).toArray(String[][]::new);
2312
}
2413

25-
/**
26-
* 131. 分割回文串
27-
* https://leetcode.cn/problems/palindrome-partitioning/
28-
*/
29-
public List<List<String>> partition131(String s) {
30-
List<List<String>> resList = new ArrayList<>();
31-
helper(s, 0, new LinkedList<>(), resList);
32-
return resList;
33-
}
14+
static class V1 {
15+
String s;
16+
List<List<String>> ans;
3417

35-
private void helper(String s, int start, LinkedList<String> subStrings, List<List<String>> resList) {
36-
if (start == s.length()) {
37-
resList.add(new ArrayList<>(subStrings));
38-
return;
18+
public List<List<String>> partition(String s) {
19+
this.s = s;
20+
ans = new ArrayList<>();
21+
backtrack(0, new ArrayList<>());
22+
return ans;
3923
}
40-
for (int i = start; i < s.length(); i++) {
41-
if (isPal(s, start, i)) {
42-
subStrings.add(s.substring(start, i + 1));
43-
helper(s, i + 1, subStrings, resList);
44-
subStrings.removeLast();
45-
}
4624

25+
// 考虑 s[i] ~ s[n-1] 怎么分割
26+
private void backtrack(int i, List<String> path) {
27+
if (i == s.length()) {
28+
ans.add(new ArrayList<>(path));
29+
return;
30+
}
31+
for (int j = i; j < s.length(); j++) {
32+
if (isPal(i, j)) {
33+
path.add(s.substring(i, j + 1));
34+
backtrack(j + 1, path);
35+
path.removeLast();
36+
}
37+
}
4738
}
48-
}
4939

50-
private boolean isPal(String s, int start, int end) {
51-
while (start < end) {
52-
if (s.charAt(start) != s.charAt(end)) {
53-
return false;
40+
private boolean isPal(int l, int r) {
41+
while (l < r) {
42+
if (s.charAt(l++) != s.charAt(r--)) return false;
5443
}
55-
start++;
56-
end--;
44+
return true;
5745
}
58-
return true;
5946
}
6047
}
6148
/*

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
public class Solution132 {
2-
public int minCut(String s) {
3-
int len = s.length();
4-
boolean[][] isPal = new boolean[len][len];
5-
for (int i = 0; i < len; i++) {
2+
public int minCut(String S) {
3+
int n = S.length();
4+
char[] s = S.toCharArray();
5+
// 预处理
6+
boolean[][] isPal = new boolean[n][n];
7+
for (int i = 0; i < n; i++) {
68
for (int j = 0; j <= i; j++) {
7-
char ch1 = s.charAt(i);
8-
char ch2 = s.charAt(j);
9-
if (ch1 == ch2 && (i <= j + 1 || isPal[j + 1][i - 1])) {
9+
if (s[i] == s[j] && (i - 1 <= j || isPal[j + 1][i - 1])) {
1010
isPal[j][i] = true;
1111
}
1212
}
1313
}
14-
// dp
15-
int[] dp = new int[len];
16-
for (int i = 0; i < len; i++) {
17-
if (isPal[0][i]) {
18-
dp[i] = 0;
19-
} else {
20-
dp[i] = i;
14+
// f[i] 表示从下标 0 到 i 的子字符串符合条件的最少分割次数,则问题解为 f[n-1]
15+
int[] f = new int[n];
16+
for (int i = 0; i < n; i++) {
17+
if (!isPal[0][i]) {
18+
f[i] = i;
2119
for (int j = 1; j <= i; j++) {
2220
if (isPal[j][i]) {
23-
dp[i] = Math.min(dp[i], dp[j - 1] + 1);
21+
f[i] = Math.min(f[i], f[j - 1] + 1);
2422
}
2523
}
2624
}
2725
}
28-
return dp[len - 1];
26+
return f[n - 1];
2927
}
3028
}
3129
/*

‎leetcode/leetcode-02/src/test/java/Solution131Tests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
import java.util.List;
55

66
public class Solution131Tests {
7-
private final Solution131 solution131 = new Solution131();
7+
private final Solution131.V1 solution131 = new Solution131.V1();
8+
private final Solution131 solutionLCR086 = new Solution131();
89

910
@Test
1011
public void example1() {
1112
String s = "aab";
1213
List<List<String>> expected = UtUtils.stringToStringList2("""
1314
[["a","a","b"],["aa","b"]]
1415
""");
15-
Assertions.assertEquals(expected, solution131.partition131(s));
16+
Assertions.assertEquals(expected, solution131.partition(s));
1617
}
1718

1819
@Test
@@ -21,7 +22,7 @@ public void example2() {
2122
List<List<String>> expected = UtUtils.stringToStringList2("""
2223
[["a"]]
2324
""");
24-
Assertions.assertEquals(expected, solution131.partition131(s));
25+
Assertions.assertEquals(expected, solution131.partition(s));
2526
}
2627

2728
// 剑指 Offer II 086. 分割回文子字符串
@@ -32,7 +33,7 @@ public void example1_2() {
3233
String[][] expected = UtUtils.stringToStrings2("""
3334
[["g","o","o","g","l","e"],["g","oo","g","l","e"],["goog","l","e"]]
3435
""");
35-
Assertions.assertArrayEquals(expected, solution131.partition(s));
36+
Assertions.assertArrayEquals(expected, solutionLCR086.partition(s));
3637
}
3738

3839
@Test
@@ -41,7 +42,7 @@ public void example2_2() {
4142
String[][] expected = UtUtils.stringToStrings2("""
4243
[["a","a","b"],["aa","b"]]
4344
""");
44-
Assertions.assertArrayEquals(expected, solution131.partition(s));
45+
Assertions.assertArrayEquals(expected, solutionLCR086.partition(s));
4546
}
4647

4748
@Test
@@ -50,6 +51,6 @@ public void example3_2() {
5051
String[][] expected = UtUtils.stringToStrings2("""
5152
[["a"]]
5253
""");
53-
Assertions.assertArrayEquals(expected, solution131.partition(s));
54+
Assertions.assertArrayEquals(expected, solutionLCR086.partition(s));
5455
}
5556
}
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.List;
4+
15
public class Solution498 {
26
public int[] findDiagonalOrder(int[][] mat) {
37
int m = mat.length;
48
int n = mat[0].length;
59

6-
int[] ans = new int[m * n];
7-
int j = 0;
8-
for (int i = 0; i < m + n - 1; i++) {
9-
if (i % 2 == 1) {
10-
// 左上到右下
11-
int x = i < n ? 0 : i - n + 1;
12-
int y = i < n ? i : n - 1;
13-
while (x < m && y >= 0) {
14-
ans[j++] = mat[x++][y--];
15-
}
16-
} else {
17-
int x = i < m ? i : m - 1;
18-
int y = i < m ? 0 : i - m + 1;
19-
while (x >= 0 && y < n) {
20-
ans[j++] = mat[x--][y++];
21-
}
10+
List<Integer> ans = new ArrayList<>();
11+
// 共有 m+n-1 条(主/次)对角线,k 的范围 [1, m+n-1]
12+
// 主对角线(左上到右下) 令 k=i+j+1 左上角 k=1 右下角 k=m+n-1, j=k-i-1, i=k-j-1
13+
// 次对角线(右上到左下) 令 k=i-j+n 右上角 k=1 左下角 k=m+n-1, j=i-k+n, i=k+j-n
14+
for (int k = 1; k <= m + n - 1; k++) {
15+
int min_j = Math.max(k - 1 - (m - 1), 0);
16+
int max_j = Math.min(k - 1, n - 1);
17+
18+
List<Integer> a = new ArrayList<>();
19+
for (int j = min_j; j <= max_j; j++) {
20+
int i = k - j - 1;
21+
a.add(mat[i][j]);
2222
}
23+
24+
if (k % 2 == 0) Collections.reverse(a);
25+
ans.addAll(a);
2326
}
24-
return ans;
27+
return ans.stream().mapToInt(Integer::intValue).toArray();
2528
}
2629
}
2730
/*
@@ -38,4 +41,6 @@ public int[] findDiagonalOrder(int[][] mat) {
3841
3942
模拟。
4043
时间复杂度 O(mn)。
44+
相似题目: 3446. 按对角线进行矩阵排序
45+
https://leetcode.cn/problems/sort-matrix-by-diagonals/description/
4146
*/

‎leetcode/leetcode-21/src/main/java/Solution2012.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
public class Solution2012 {
22
public int sumOfBeauties(int[] nums) {
3-
int len = nums.length;
4-
int res = 0;
5-
for (int i = 1; i <= len - 2; i++) {
3+
int n = nums.length;
4+
int ans = 0;
5+
for (int i = 1; i <= n - 2; i++) {
66
if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) {
7-
res++;
7+
ans++;
88
}
99
}
1010
// leftMax[i] 表示 nums[0, i] 的最大值
11-
int[] leftMax = new int[len];
11+
int[] leftMax = new int[n];
1212
leftMax[0] = nums[0];
13-
for (int i = 1; i < len; i++) {
13+
for (int i = 1; i < n; i++) {
1414
leftMax[i] = Math.max(leftMax[i - 1], nums[i]);
1515
}
1616
// rightMin[i] 表示 nums[i, len-1] 的最小值
17-
int[] rightMin = new int[len];
18-
rightMin[len - 1] = nums[len - 1];
19-
for (int i = len - 2; i >= 0; i--) {
17+
int[] rightMin = new int[n];
18+
rightMin[n - 1] = nums[n - 1];
19+
for (int i = n - 2; i >= 0; i--) {
2020
rightMin[i] = Math.min(rightMin[i + 1], nums[i]);
2121
}
22-
for (int i = 1; i <= len - 2; i++) {
22+
for (int i = 1; i <= n - 2; i++) {
2323
if (leftMax[i - 1] < nums[i] && nums[i] < rightMin[i + 1]) {
24-
res++;
24+
ans++;
2525
}
2626
}
27-
return res;
27+
return ans;
2828
}
2929
}
3030
/*

0 commit comments

Comments
(0)

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