5.最长回文子串
作者:
康大侠 | 来源:发表于
2021-05-24 10:19 被阅读0次
![](https://img.haomeiwen.com/i328144/43e765fbf4851773.png)
最长回文子串.png
![](https://img.haomeiwen.com/i328144/711a1ad37143dd12.png)
动态规划
class Solution {
public String longestPalindrome(String s) {
char[] cs = s.toCharArray();
boolean[][] dp = new boolean[cs.length][cs.length];
int maxLen = 1;
int begin = 0;
for (int i = cs.length - 1; i >= 0; i--) {
for (int j = i; j < cs.length; j++ ) {
int len = j - i + 1;
dp[i][j] = (cs[i] == cs[j]) && (len <= 2 || dp[i+1][j-1]);
if (dp[i][j] && len > maxLen) {
maxLen = len;
begin = i;
}
}
}
return new String(cs,begin,maxLen);
}
}
本文标题:5.最长回文子串
本文链接:https://www.haomeiwen.com/subject/xxxxsltx.html
网友评论