-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxArray.java
More file actions
40 lines (36 loc) · 1.09 KB
/
MaxArray.java
File metadata and controls
40 lines (36 loc) · 1.09 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
/**
* 获得字符串的最长回文串
*/
public class MaxArray {
public static void main(String[] args) {
String str = "aaabccc";
System.out.println(getMaxArray(str));
}
public static boolean isSymmetric(String mString) {
int i = 0;
int j = mString.length() - 1;
while (i < j) {
if (mString.charAt(i++) != mString.charAt(j--)) {
return false;
}
}
return true;
}
public static String getMaxArray(String mString) {
if (isSymmetric(mString)) return mString;
String result = null;
int length;
int maxLength = Integer.MIN_VALUE;
for (int i = mString.length(); i > 1; i--) {
for (int j = 0; j < i; j++) {
String temp = mString.substring(j, i);
length = temp.length();
if (isSymmetric(temp) && maxLength < length) {
result = temp;
maxLength = length;
}
}
}
return result;
}
}