-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lc0005Solution2.java
38 lines (36 loc) · 1.18 KB
/
Lc0005Solution2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package top;
public class Lc0005Solution2 {
public String longestPalindrome(String s) {
// dp 这么慢的吗。。。
if (s == null || s.length() == 0) {
return "";
}
int n = s.length();
boolean[][] dp = new boolean[n][n];
int start = 0, end = 0;
for (int len = 0; len < n; len++) {
for (int i = 0; i + len < n; i++) {
int j = i + len;
if (len == 0) {
dp[i][j] = true;
} else if (len == 1) {
dp[i][j] = s.charAt(i) == s.charAt(j);
} else {
dp[i][j] = dp[i + 1][j - 1] && s.charAt(i) == s.charAt(j);
}
if (dp[i][j] && j - i > end - start) {
start = i;
end = j;
}
}
}
return s.substring(start, end + 1);
}
public static void main(String[] args) {
Lc0005Solution2 solution = new Lc0005Solution2();
String s = "babad";
System.out.println(solution.longestPalindrome(s));
s = "cbbd";
System.out.println(solution.longestPalindrome(s));
}
}