-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_5.java
More file actions
58 lines (50 loc) · 1.71 KB
/
Solution_5.java
File metadata and controls
58 lines (50 loc) · 1.71 KB
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
58
package org.hyz.problem;
import org.apache.commons.lang3.StringUtils;
/**
* 给你一个字符串 s,找到 s 中最长的回文子串。
* 两种情况,一种奇数, 一种偶数
*
* @author huyz
*/
public class Solution_5 {
public static void main(String[] args) {
String s = "abaa";
System.out.println(longestPalindrome(s));
}
public static String longestPalindrome(String s) {
String result = "";
if (StringUtils.isEmpty(s) || s.length() == 1) {
return result;
}
for (int i = 1; i < s.length() - 1; i++) {
String resultTempFirst = mindRise(s, i - 1, i + 1);
String resultTempSecond = mindRise(s, i, i + 1);
boolean resultTempFEmp = StringUtils.isEmpty(resultTempFirst) || resultTempFirst.length() == 1;
boolean resultTempSEmp = StringUtils.isEmpty(resultTempSecond) || resultTempSecond.length() == 1;
if (resultTempFEmp && resultTempSEmp) {
continue;
}
result = result.length() > resultTempFirst.length() ? result : resultTempFirst;
result = result.length() > resultTempSecond.length() ? result : resultTempSecond;
}
return result;
}
public static String mindRise(String s, int lowIndex, int highIndex) {
char[] array = s.toCharArray();
int i = lowIndex;
int j = highIndex;
while (i > 0 && j < s.length() - 1) {
if (array[i] == array[j]) {
i--;
j++;
} else {
break;
}
}
if (array[i] != array[j]) {
i++;
j--;
}
return s.substring(i, j + 1);
}
}