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 fe8a245

Browse files
committed
clean code
1 parent bc075d2 commit fe8a245

File tree

32 files changed

+565
-286
lines changed

32 files changed

+565
-286
lines changed

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,18 @@ public int uniquePathsWithObstacles(int[][] obstacleGrid) {
33
int m = obstacleGrid.length;
44
int n = obstacleGrid[0].length;
55

6-
if (obstacleGrid[0][0] == 1) {
7-
return 0;
8-
}
9-
6+
if (obstacleGrid[0][0] == 1) return 0;
107
// f[i][j] 表示到达坐标 (i,j) 的路径总数
118
int[][] f = new int[m][n];
129
// 初始状态
1310
f[0][0] = 1;
1411
for (int i = 1; i < m; i++) {
15-
if (obstacleGrid[i][0] == 0) {
16-
f[i][0] = 1;
17-
} else {
18-
break;
19-
}
12+
if (obstacleGrid[i][0] != 0) break;
13+
f[i][0] = 1;
2014
}
2115
for (int j = 1; j < n; j++) {
22-
if (obstacleGrid[0][j] == 0) {
23-
f[0][j] = 1;
24-
} else {
25-
break;
26-
}
16+
if (obstacleGrid[0][j] != 0) break;
17+
f[0][j] = 1;
2718
}
2819
// 状态转移
2920
for (int i = 1; i < m; i++) {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ public ListNode partition(ListNode head, int x) {
2727
}
2828
return dummy.next;
2929
}
30+
31+
public ListNode partition2(ListNode head, int x) {
32+
ListNode small = new ListNode(0);
33+
ListNode smallHead = small;
34+
ListNode large = new ListNode(0);
35+
ListNode largeHead = large;
36+
while (head != null) {
37+
if (head.val < x) {
38+
small.next = head;
39+
small = small.next;
40+
} else {
41+
large.next = head;
42+
large = large.next;
43+
}
44+
head = head.next;
45+
}
46+
large.next = null;
47+
small.next = largeHead.next;
48+
return smallHead.next;
49+
}
3050
}
3151
/*
3252
86. 分隔链表

‎leetcode/leetcode-01/src/test/java/Solution86Tests.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public void example1() {
1010
int x = 3;
1111
ListNode expected = ListNode.buildListNode(new int[]{1, 2, 2, 4, 3, 5});
1212
Assertions.assertTrue(ListNode.assertListNodeEquals(expected, solution86.partition(head, x)));
13+
Assertions.assertTrue(ListNode.assertListNodeEquals(expected, solution86.partition2(head, x)));
1314
}
1415

1516
@Test
@@ -18,5 +19,6 @@ public void example2() {
1819
int x = 2;
1920
ListNode expected = ListNode.buildListNode(new int[]{1, 2});
2021
Assertions.assertTrue(ListNode.assertListNodeEquals(expected, solution86.partition(head, x)));
22+
Assertions.assertTrue(ListNode.assertListNodeEquals(expected, solution86.partition2(head, x)));
2123
}
2224
}

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
1+
import java.util.Arrays;
2+
13
public class Solution122 {
4+
private int[] prices;
5+
private int[][] memo;
6+
27
public int maxProfit(int[] prices) {
8+
this.prices = prices;
9+
int n = prices.length;
10+
memo = new int[n][2];
11+
for (int i = 0; i < n; i++) {
12+
Arrays.fill(memo[i], -1);
13+
}
14+
return dfs(n - 1, 0);
15+
}
16+
17+
// dfs(i, 0) 表示到第 i 天结束时,未持有股票的最大利润
18+
// dfs(i, 1) 表示到第 i 天结束时,持有股票的最大利润
19+
// 第 i-1 天结束就是第 i 天的开始
20+
private int dfs(int i, int hold) {
21+
if (i < 0) {
22+
if (hold == 1) return (int) -1e9;
23+
return 0;
24+
}
25+
if (memo[i][hold] != -1) return memo[i][hold];
26+
int res;
27+
if (hold == 1) res = Math.max(dfs(i - 1, 1), dfs(i - 1, 0) - prices[i]);
28+
else res = Math.max(dfs(i - 1, 0), dfs(i - 1, 1) + prices[i]);
29+
return memo[i][hold] = res;
30+
}
31+
32+
public int maxProfit2(int[] prices) {
333
int res = 0;
434
for (int i = 0; i < prices.length - 1; i++) {
535
// 贪心。累加所有差值为正数的交易
@@ -19,6 +49,12 @@ public int maxProfit(int[] prices) {
1949
1 <= prices.length <= 3 * 10^4
2050
0 <= prices[i] <= 10^4
2151
22-
贪心。只要明天比今天涨的都进行交易。
52+
状态机 DP / 贪心。只要明天比今天涨的都进行交易。
2353
系列题解 https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/solution/tan-xin-suan-fa-by-liweiwei1419-2/
54+
相似题目: 309. 最佳买卖股票时机含冷冻期
55+
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-cooldown/
56+
714. 买卖股票的最佳时机含手续费
57+
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
58+
188. 买卖股票的最佳时机 IV
59+
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iv/
2460
*/
Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
1-
public class Solution188 {
2-
public int maxProfit(int k, int[] prices) {
3-
int len = prices.length;
4-
if (len == 0) {
5-
return 0;
6-
}
7-
8-
// 因为 n 天最多只能进行 floor(len/2) 笔交易
9-
k = Math.min(k, len / 2);
10-
11-
// buy[i][j] 表示对于数组 prices[0,i] 中的价格而言,进行恰好 j 笔交易,并且当前手上持有一支股票,这种情况下的最大利润
12-
// sell[i][j] 表示恰好进行 j 笔交易,并且当前手上不持有股票,这种情况下的最大利润
13-
int[][] buy = new int[len][k + 1];
14-
int[][] sell = new int[len][k + 1];
1+
import java.util.Arrays;
152

16-
// 初始状态
17-
buy[0][0] = -prices[0];
18-
sell[0][0] = 0;
19-
for (int i = 1; i <= k; ++i) {
20-
buy[0][i] = sell[0][i] = Integer.MIN_VALUE / 2;
21-
}
3+
public class Solution188 {
4+
private int[] prices;
5+
private int[][][] memo;
226

23-
// 状态转移
24-
for (int i = 1; i < len; ++i) {
25-
buy[i][0] = Math.max(buy[i - 1][0], sell[i - 1][0] - prices[i]);
26-
for (int j = 1; j <= k; ++j) {
27-
buy[i][j] = Math.max(buy[i - 1][j], sell[i - 1][j] - prices[i]);
28-
sell[i][j] = Math.max(sell[i - 1][j], buy[i - 1][j - 1] + prices[i]);
7+
public int maxProfit(int k, int[] prices) {
8+
this.prices = prices;
9+
int n = prices.length;
10+
memo = new int[n][k + 1][2];
11+
for (int i = 0; i < n; i++) {
12+
for (int j = 0; j < k + 1; j++) {
13+
Arrays.fill(memo[i][j], -1);
2914
}
3015
}
16+
return dfs(n - 1, k, 0);
17+
}
3118

32-
int max = 0;
33-
for (int x : sell[len - 1]) {
34-
max = Math.max(max, x);
19+
// dfs(i, 0) 表示到第 i 天结束时完成至多 j 笔交易,未持有股票的最大利润
20+
// dfs(i, 1) 表示到第 i 天结束时完成至多 j 笔交易,持有股票的最大利润
21+
// 第 i-1 天结束就是第 i 天的开始
22+
private int dfs(int i, int j, int hold) {
23+
if (j < 0) return (int) -1e9;
24+
if (i < 0) {
25+
if (hold == 1) return (int) -1e9;
26+
return 0;
3527
}
36-
return max;
28+
if (memo[i][j][hold] != -1) return memo[i][j][hold];
29+
int res;
30+
if (hold == 1) res = Math.max(dfs(i - 1, j, 1), dfs(i - 1, j - 1, 0) - prices[i]);
31+
else res = Math.max(dfs(i - 1, j, 0), dfs(i - 1, j, 1) + prices[i]);
32+
return memo[i][j][hold] = res;
3733
}
3834
}
3935
/*
@@ -48,5 +44,5 @@ public int maxProfit(int k, int[] prices) {
4844
0 <= prices.length <= 1000
4945
0 <= prices[i] <= 1000
5046
51-
动态规划
47+
状态机 DP
5248
*/

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ public void example1() {
99
int[] prices = {7, 1, 5, 3, 6, 4};
1010
int expected = 7;
1111
Assertions.assertEquals(expected, solution122.maxProfit(prices));
12+
Assertions.assertEquals(expected, solution122.maxProfit2(prices));
1213
}
1314

1415
@Test
1516
public void example2() {
1617
int[] prices = {1, 2, 3, 4, 5};
1718
int expected = 4;
1819
Assertions.assertEquals(expected, solution122.maxProfit(prices));
20+
Assertions.assertEquals(expected, solution122.maxProfit2(prices));
1921
}
2022

2123
@Test
2224
public void example3() {
2325
int[] prices = {7, 6, 4, 3, 1};
2426
int expected = 0;
2527
Assertions.assertEquals(expected, solution122.maxProfit(prices));
28+
Assertions.assertEquals(expected, solution122.maxProfit2(prices));
2629
}
2730
}
Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
1-
import java.util.ArrayDeque;
21
import java.util.ArrayList;
3-
import java.util.HashMap;
2+
import java.util.Arrays;
43
import java.util.List;
5-
import java.util.Map;
6-
import java.util.Queue;
74

85
public class Solution207 {
96
public boolean canFinish(int numCourses, int[][] prerequisites) {
10-
Map<Integer, List<Integer>> adj = new HashMap<>();
11-
int[] inDeg = new int[numCourses];
12-
for (int[] prerequisite : prerequisites) {
13-
int ai = prerequisite[1];
14-
int bi = prerequisite[0];
15-
// 其中 prerequisites[i] = [ai, bi] ,表示在选修课程 ai 前 必须 先选修 bi 。
16-
adj.computeIfAbsent(ai, key -> new ArrayList<>()).add(bi);
17-
inDeg[bi]++;
7+
List<Integer>[] g = new ArrayList[numCourses];
8+
Arrays.setAll(g, e -> new ArrayList<>());
9+
for (int[] p : prerequisites) {
10+
int x = p[1], y = p[0];
11+
g[x].add(y);
1812
}
1913

20-
// 拓扑排序
21-
Queue<Integer> queue = new ArrayDeque<>();
22-
for (int id = 0; id < numCourses; id++) {
23-
if (inDeg[id] == 0) {
24-
queue.add(id);
25-
}
14+
int[] colors = new int[numCourses];
15+
for (int i = 0; i < numCourses; i++) {
16+
if (colors[i] == 0 && dfs(i, g, colors)) return false; // 有环
2617
}
27-
List<Integer> resList = new ArrayList<>();
28-
while (!queue.isEmpty()) {
29-
int x = queue.remove();
30-
resList.add(x);
31-
for (int y : adj.getOrDefault(x, new ArrayList<>())) {
32-
inDeg[y]--;
33-
if (inDeg[y] == 0) {
34-
queue.add(y);
35-
}
36-
}
18+
return true; // 没有环
19+
}
20+
21+
private boolean dfs(int x, List<Integer>[] g, int[] colors) {
22+
colors[x] = 1; // x 正在访问中
23+
for (int y : g[x]) {
24+
if (colors[y] == 1) return true;
25+
if (colors[y] == 0 && dfs(y, g, colors)) return true;
3726
}
38-
return resList.size() == numCourses;
27+
colors[x] = 2; // x 完全访问完毕
28+
return false; // 没有找到环
3929
}
4030
}
4131
/*
@@ -54,7 +44,11 @@ public boolean canFinish(int numCourses, int[][] prerequisites) {
5444
0 <= ai, bi < numCourses
5545
prerequisites[i] 中的所有课程对 互不相同
5646
57-
拓扑排序。
47+
三色标记法 判环
48+
0 没有遍历过
49+
1 正在遍历中
50+
2 遍历过了
51+
另有 拓扑排序 解法。
5852
相似题目: 210. 课程表 II
5953
https://leetcode.cn/problems/course-schedule-ii/
6054
*/

0 commit comments

Comments
(0)

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