Skip to content

Commit 2b11bc6

Browse files
committed
fd
1 parent 2726063 commit 2b11bc6

File tree

2 files changed

+20
-78
lines changed

2 files changed

+20
-78
lines changed

leetcode/solution/src/TrappingRainWater.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,19 @@ public class TrappingRainWater {
55

66
// 耗时24ms
77
public int trap(int[] height) {
8-
int len = height.length;
8+
int[] left = new int[height.length];
9+
int[] right = new int[height.length];
910

10-
if (len == 0) {
11-
return 0;
12-
}
13-
14-
int[] left = new int[len];
15-
int[] right = new int[len];
16-
17-
left[0] = 0;
18-
right[len - 1] = 0;
19-
20-
for (int i = 1; i < len; i++) {
11+
for (int i = 1; i < height.length; i++) {
2112
left[i] = Math.max(left[i - 1], height[i - 1]);
2213
}
23-
for (int i = len - 2; i>= 0; i--) {
14+
for (int i = height.length - 2; i >= 0; i--) {
2415
right[i] = Math.max(right[i + 1], height[i + 1]);
2516
}
26-
int sum = 0;
27-
for (int i = 0; i < len; i++) {
28-
int high = Math.min(left[i], right[i]);
29-
if (high > height[i]) {
30-
sum += high - height[i];
31-
}
17+
int rain = 0;
18+
for (int i = 1; i < height.length - 1; i++) {
19+
rain += Math.max(0, Math.min(left[i], right[i]) - height[i]);
3220
}
33-
return sum;
21+
return rain;
3422
}
3523
}

leetcode/src/Main.java

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,23 @@
22

33
public class Main {
44

5-
public String longestPalindrome(String s) {
6-
int[] res = new int[2];
7-
for (int i = 0; i < s.length(); i++) {
8-
helper(s, i - 1, i + 1, res);
9-
helper(s, i, i + 1, res);
10-
}
11-
return s.substring(res[1], res[1] + res[0]);
12-
}
5+
public int trap(int[] height) {
6+
int[] left = new int[height.length];
7+
int[] right = new int[height.length];
138

14-
private void helper(String s, int left, int right, int[] res) {
15-
for ( ; left >= 0 && right < s.length(); left--, right++) {
16-
if (s.charAt(left) != s.charAt(right)) {
17-
break;
18-
}
9+
for (int i = 1; i < height.length; i++) {
10+
left[i] = Math.max(left[i - 1], height[i - 1]);
1911
}
20-
int len = --right - ++left + 1;
21-
if (len > res[0]) {
22-
res[0] = len;
23-
res[1] = left;
12+
for (int i = height.length - 2; i >= 0; i--) {
13+
right[i] = Math.max(right[i + 1], height[i + 1]);
2414
}
15+
int rain = 0;
16+
for (int i = 1; i < height.length - 1; i++) {
17+
rain += Math.max(0, Math.min(left[i], right[i]) - height[i]);
18+
}
19+
return rain;
2520
}
2621

27-
// public static String longestPalindrome(String s) {
28-
// if (s.length() == 0) {
29-
// return "";
30-
// }
31-
// int max = 0, idx = -1;
32-
// for (int i = 0; i < s.length(); i++) {
33-
// int len = findLen(s, i);
34-
// if (len > max) {
35-
// max = len;
36-
// idx = i;
37-
// }
38-
// }
39-
// if (max % 2 == 0) {
40-
// return s.substring(idx - max / 2 + 1, idx + max / 2 + 1);
41-
// } else {
42-
// return s.substring(idx - max / 2, idx + max / 2 + 1);
43-
// }
44-
// }
45-
//
46-
// private static int findLen(String s, int index) {
47-
// int len1 = 1;
48-
// for (int i = 1; i <= s.length(); i++) {
49-
// int i1 = index - i, i2 = index + i;
50-
// if (i1 < 0 || i2 >= s.length() || s.charAt(i1) != s.charAt(i2)) {
51-
// break;
52-
// }
53-
// len1 += 2;
54-
// }
55-
// int len2 = 0;
56-
// for (int i = 0; i < s.length(); i++) {
57-
// int i1 = index - i, i2 = index + i + 1;
58-
// if (i1 < 0 || i2 >= s.length() || s.charAt(i1) != s.charAt(i2)) {
59-
// break;
60-
// }
61-
// len2 += 2;
62-
// }
63-
// return Math.max(len1, len2);
64-
// }
65-
6622
public static void main(String[] args) {
67-
String s = longestPalindrome("cbbd");
68-
System.out.println(s);
6923
}
7024
}

0 commit comments

Comments
 (0)