-
Notifications
You must be signed in to change notification settings - Fork 19
/
LongestPalindromicSubstring.java
50 lines (45 loc) · 1.24 KB
/
LongestPalindromicSubstring.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
package oj.leetcode;
/**
* 5. Longest Palindromic Substring
* Created by vonzhou on 2019/1/15.
*/
public class LongestPalindromicSubstring {
/**
* 我能想到的暴力法
*
* @param s
* @return
*/
public String longestPalindrome(String s) {
if (s == null || s.length() <= 1) {
return s;
}
int start = 0, end = 0;
char[] cs = s.toCharArray();
int max = 0;
for (int i = 0; i < cs.length; i++) {
for (int j = i; j < cs.length; j++) {
if (isPalindrome(cs, i, j)) {
int len = j - i + 1;
if (len > max) {
max = len;
start = i;
end = j;
}
}
}
}
return s.substring(start, end + 1);
}
private boolean isPalindrome(char[] cs, int start, int end) {
for (int i = start, j = end; i < j; ++i, --j) {
if (cs[i] != cs[j]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(new LongestPalindromicSubstring().longestPalindrome("babad"));
}
}