Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5aa7acb

Browse files
committed
增加No5
1 parent 830c694 commit 5aa7acb

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

String/No5.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @author tujietg
3+
* @date 5/21/20 9:07 AM
4+
*/
5+
public class No5 {
6+
public String longestPalindrome(String s) {
7+
int len = s.length();
8+
if (len < 2) {
9+
return s;
10+
}
11+
int start = 0;
12+
int midLen = 1;
13+
14+
boolean[][] bp = new boolean[len][len];
15+
for (int i = 0; i < len; i++) {
16+
bp[i][i] = true;
17+
}
18+
19+
char[] charArr = s.toCharArray();
20+
for (int j = 1; j < len; j++) {
21+
for (int i = 0; i < j; i++) {
22+
if (charArr[i] != charArr[j]) {
23+
bp[i][j] = false;
24+
} else {
25+
if (j - i < 3) {
26+
bp[i][j] = true;
27+
} else {
28+
bp[i][j] = bp[i + 1][j - 1];
29+
}
30+
}
31+
if (bp[i][j] == true && j - i + 1 > midLen) {
32+
midLen = j - i + 1;
33+
start = i;
34+
}
35+
}
36+
}
37+
return s.substring(start, start + midLen);
38+
}
39+
}

0 commit comments

Comments
 (0)