-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL5.java
More file actions
55 lines (46 loc) · 1.28 KB
/
L5.java
File metadata and controls
55 lines (46 loc) · 1.28 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
package com.liang.leetcode;
/**
* @ClassName L5
* @description longest-palindromic-substring
* @Author LiaNg
* @Date 2018/12/6
*/
public class L5 {
public static void main(String[] args) {
String str = "bababd";
L5 l = new L5();
System.out.println(l.longestPalindrome(str));
}
/**
* 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
*/
private String longestPalindrome(String s) {
int n = s.length();
if (n <= 1) {
return s;
}
String longest = "";
String str;
for (int i = 0; i < n - 1; i++) {
str = findPalindrome(s, i, i);
if (str.length() > longest.length()) {
longest = str;
}
str = findPalindrome(s, i, i + 1);
if (str.length() > longest.length()) {
longest = str;
}
}
return longest;
}
private String findPalindrome(String str, int left, int right) {
int n = str.length();
int l = left;
int r = right;
while (l >= 0 && r <= n - 1 && str.charAt(l) == str.charAt(r)) {
l--;
r++;
}
return str.substring(l + 1, r);
}
}