-
Notifications
You must be signed in to change notification settings - Fork 1
/
java_1_6_1.java
57 lines (52 loc) · 1.73 KB
/
java_1_6_1.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package chapter1;
/**
* 最长回文子串
*/
public class java_1_6_1 {
public static void main(String[] argv) {
String str = "abba";
System.out.println(longestPalindrome(str));
}
//对每一个点,都求它的最长回文串的长度,区分奇数偶数
//另一种解法Manacher以后有机会再看
private static int longestPalindrome(String str) {
if (str == null || str.length() == 0) {
return 0;
}
int center = 0;
int max = 1, currentLength = 0;
for (int i = 0; i < str.length(); i++) {
//回文长度为奇数
for (int j = 0; (i - j) >= 0 && (i + j) < str.length(); j++) {
if (str.charAt(i - j) != str.charAt(i + j)) {
break;
}
currentLength = j * 2 + 1;
}
if (currentLength > max) {
max = currentLength;
center = i;
}
//回文长度为偶数
for (int j = 0; (i - j) >= 0 && (i + j + 1) < str.length(); j++) {
if (str.charAt(i - j) != str.charAt(i + j + 1)) {
break;
}
currentLength = j * 2 + 2;
}
if (currentLength > max) {
max = currentLength;
center = i;
}
}
System.out.println("center " + center + " max " + max);
int startPos = center - ((max - 1) >> 1);
int endPos = startPos + max - 1;
StringBuilder sb = new StringBuilder();
for (int i = startPos; i <= endPos; i++) {
sb.append(str.charAt(i));
}
System.out.println(sb.toString());
return max;
}
}