Skip to content

Commit d6bf5e7

Browse files
committed
fd
1 parent c46a5d6 commit d6bf5e7

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

leetcode/solution/src/LinkedListCycle.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
/**
2-
* https://leetcode.com/articles/linked-list-cycle/
3-
*/
4-
51
public class LinkedListCycle {
62

3+
// 注意判空
74
public boolean hasCycle(ListNode head) {
85
for (ListNode fast = head, slow = head; fast != null && fast.next != null; ) {
96
slow = slow.next;

leetcode/solution/src/MaximumSubarray.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,23 @@ public class MaximumSubarray {
22

33
// dp[i]表示包含第i个元素时的最大和
44
public int maxSubArray(int[] nums) {
5-
if (nums.length == 0) {
6-
return 0;
7-
}
85
int[] dp = new int[nums.length];
96
int max = Integer.MIN_VALUE;
107
for (int i = 0; i < nums.length; i++) {
11-
dp[i] = nums[i];
12-
if (i > 0 && dp[i - 1] > 0) {
13-
dp[i] += dp[i - 1];
14-
}
8+
dp[i] = Math.max(i > 0 ? dp[i - 1] + nums[i] : nums[i], nums[i]);
159
max = Math.max(max, dp[i]);
1610
}
1711
return max;
1812
}
1913

20-
public int maxSubArray2(int[] nums) {
14+
/* 这里dp其实可以去掉,换成一个普通int变量即可
15+
public int maxSubArray(int[] nums) {
2116
int max = Integer.MIN_VALUE, prev = 0;
2217
for (int i = 0; i < nums.length; i++) {
23-
prev = Math.max(nums[i], nums[i] + prev);
18+
prev = Math.max(i > 0 ? prev + nums[i] : nums[i], nums[i]);
2419
max = Math.max(max, prev);
2520
}
2621
return max;
2722
}
23+
*/
2824
}

leetcode/src/Main.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
public class Main {
44

5-
public int maxArea(int[] height) {
6-
int max = 0;
7-
for (int i = 0, j = height.length - 1; i < j; ) {
8-
max = Math.max(max, Math.min(height[i], height[j]) * (j - i));
9-
if (height[i] < height[j]) {
10-
i++;
11-
} else {
12-
j--;
13-
}
5+
public int maxSubArray(int[] nums) {
6+
int[] dp = new int[nums.length];
7+
int max = Integer.MIN_VALUE;
8+
for (int i = 0; i < nums.length; i++) {
9+
dp[i] = Math.max(i > 0 ? dp[i - 1] + nums[i] : nums[i], nums[i]);
10+
max = Math.max(max, dp[i]);
11+
}
12+
return max;
13+
}
14+
15+
public int maxSubArray2(int[] nums) {
16+
int max = Integer.MIN_VALUE, prev = 0;
17+
for (int i = 0; i < nums.length; i++) {
18+
prev = Math.max(i > 0 ? prev + nums[i] : nums[i], nums[i]);
19+
max = Math.max(max, prev);
1420
}
1521
return max;
1622
}

0 commit comments

Comments
 (0)