-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.最长回文子串.java
66 lines (64 loc) · 2.08 KB
/
5.最长回文子串.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
58
59
60
61
62
63
64
65
/*
* @lc app=leetcode.cn id=5 lang=java
*
* [5] 最长回文子串
*/
// @lc code=start
//动态规划方法
/*
class Solution {
public String longestPalindrome(String s) {
int n = s.length();
boolean [][] dp = new boolean [n][n];
String ans = "";
for(int l = 0 ; l < n ; l++){ //l用来记录当前可能的字串长度
for(int i=0;i<n-l;i++){
int j = i+l;
if(l == 0){ //当前可能字串长度为1,则一定是回文子串
dp[i][j] = true;
}else if(l == 1){ // 当前可能字串长度为2,则在两个字母相同的情况下为真
dp[i][j] = (s.charAt(i) == s.charAt(j));
}else { //当前可能字串长度大于2,则利用状态转移公式判断是否为回文字串
dp[i][j] = (s.charAt(i) == s.charAt(j) && dp[i+1][j-1]);
}
if(l + 1 > ans.length() && dp[i][j]){
ans = s.substring(i,i + l + 1);
}
}
}
return ans;
}
}*/
//中心扩展方法
class Solution {
public String longestPalindrome(String s) {
if(s == null || s.length() < 1){
return "";
}
int n = s.length();
int len = 0;
int left = 0,right = 0;
String ans = "";
for(int i = 0;i<n;i++){
int len1 = expandAroundCenter(s,i,i);
int len2 = expandAroundCenter(s,i,i+1);
len = Math.max(len1,len2);
if(len > right - left){
//边界控制!!!
left = i - (len - 1)/2;
right = i + len / 2;
}
}
//substring结束索引不包括,所以right+1
return s.substring(left,right + 1);
}
public int expandAroundCenter(String s,int left,int right){
while(left >=0 && right < s.length() && s.charAt(left) == s.charAt(right)){
left--;
right++;
}
//精准控制边界条件
return right - left - 1;
}
}
// @lc code=end