题目链接:https://leetcode.com/problems/longest-palindromic-substring/description/

题目大意:找出最长回文子字符串(连续)。

法一:暴力,三层for循环,超时。代码如下:

 public String longestPalindrome(String s) {
String res = "";
//逐一检查每个子字符串
for(int i = 0; i < s.length(); i++) {
for(int j = i + 1; j < s.length(); j++) {
String tmp = s.substring(i, j + 1);
if(isPalindrome(tmp) == true) {
if(tmp.length() > res.length()) {
res = tmp;
}
}
}
}
if(res.length() == 0) {
res = String.valueOf(s.charAt(0));
}
return res;
}
//判断子字符串是否是回文
public static boolean isPalindrome(String s) {
for(int i = 0, j = s.length() - 1; i < j; i++, j--) {
if(s.charAt(i) != s.charAt(j)) {
return false;
}
}
return true;
}

法二(借鉴):dp,依次计算长度为1,2,3,。。。n的所有子字符串是否是回文,只是每次计算的时候都可以直接沿用上一次计算的结果,这样可以不用for循环判断,也就是减少了一层for循环。dp公式:dp[i, j]=ture表示初始下标为i,终点下标为j的字符串是回文字符串,dp[i, j]=true当且仅当dp[i+1, j-1]=true。代码如下(耗时71ms):

     public String longestPalindrome(String s) {
int length = s.length();
boolean dp[][] = new boolean[length][length];
int start = 0, maxLength = 1;
//初始化回文长度是1-2
for(int i = 0; i < length; i++) {
dp[i][i] = true;
if(i < length - 1 && s.charAt(i) == s.charAt(i + 1)) {
dp[i][i + 1] = true;
start = i;
maxLength = 2;
}
}
//计算回文长度是3-length
for(int strLength = 3; strLength <= length; strLength++) {
//计算所有长度为strLength的字符串是否是回文串
for(int i = 0; i <= length - strLength; i++) {
int j = i + strLength - 1;//子字符串终止位置
if(dp[i + 1][j - 1] == true && s.charAt(i) == s.charAt(j)) {
dp[i][j] = true;
start = i;
maxLength = strLength;
}
}
}
return s.substring(start, start + maxLength);
}

dp数组(例子:cabba计算回文):

  0("c") 1("a") 2("b") 3("b") 4("a")
0("c") T(c) F(ca) F(cab) F(cabb) F(cabba)
1("a")   T(a) F(ab) F(abb) T(abba)
2("b")     T(b) T(bb) F(bba)
3("b")       T(b) F(ba)
4("a")         T(a)

dp数组填充顺序:从左下到右上,即每一个数值计算都要用到左边,下边,左下的数据。

法三(借鉴):中心扩展法(分治法),以每个字符为中心,向两边扩展找相应的字符串是否有回文。但是,要注意两种情况,一种是aba的情况,一种是abba的情况,两种的扩展中心有点区别。代码如下(耗时67ms):

     public String longestPalindrome(String s) {
int length = s.length();
int start = 0, maxLength = 1;
//aba的情况,以i为中心扩展
for(int i = 0; i < length; i++) {
int left = i - 1, right = i + 1;
while(left >= 0 && right < length && s.charAt(left) == s.charAt(right)) {
if(right - left + 1 > maxLength) {
maxLength = right - left + 1;
start = left;
}
left--;
right++;
}
}
//abba的情况,以i, i+1为中心扩展
for(int i = 0; i < length; i++) {
int left = i, right = i + 1;
while(left >= 0 && right < length && s.charAt(left) == s.charAt(right)) {
if(right - left + 1 > maxLength) {
maxLength = right - left + 1;
start = left;
}
left--;
right++;
}
}
return s.substring(start, start + maxLength);
}

5.Longest Palindromic Substring---dp的更多相关文章

  1. *5. Longest Palindromic Substring (dp) previous blogs are helpful

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  2. 最长回文子串(Longest Palindromic Substring)-DP问题

    问题描述: 给定一个字符串S,找出它的最大的回文子串,你可以假设字符串的最大长度是1000,而且存在唯一的最长回文子串 . 思路分析: 动态规划的思路:dp[i][j] 表示的是 从i 到 j 的字串 ...

  3. Leetcode:【DP】Longest Palindromic Substring 解题报告

    Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...

  4. 5.Longest Palindromic Substring (String; DP, KMP)

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  5. 5. Longest Palindromic Substring(最长回文子串 manacher 算法/ DP动态规划)

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  6. 5. Longest Palindromic Substring (DP)

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  7. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  8. Leetcode Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  9. 【leedcode】 Longest Palindromic Substring

    Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...

  10. 5. Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

随机推荐

  1. BZOJ 1799 同类分布(数位DP)

    给出a,b,求出[a,b]中各位数字之和能整除原数的数的个数.1<=a<=b<=1e18. 注意到各位数字之和最大是153.考虑枚举这个东西.那么需要统计的是[0,a-1]和[0,b ...

  2. 【JavaScript&jQuery】轮展图

    用bootstrap实现轮展图和用Jquery自定义轮展图两种   1.使用bootstrap插件 效果图: 用一个超简单的方法,那就是用bootstrap的插件,什么?不懂bootstrap?没关系 ...

  3. 如何使用火狐下的两款接口测试工具RESTClient和HttpRequester发送post请求

    Chrome下有著名的Postman,那火狐也有它的左膀右臂,那就是RESTClient和HttpRequester.这两款工具都是火狐的插件,主要用来模拟发送HTTP请求,HTTP请求最常用的两种方 ...

  4. 【BZOJ5306】[HAOI2018]染色(NTT)

    [BZOJ5306]染色(NTT) 题面 BZOJ 洛谷 题解 我们只需要考虑每一个\(W[i]\)的贡献就好了 令\(lim=min(M,\frac{N}{S})\) 那么,开始考虑每一个\(W[i ...

  5. BZOJ 3786: 星系探索 解题报告

    3786: 星系探索 Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅 ...

  6. 虚拟机安装ubuntu14.04.5系统

    参考教程 在vitualbox安装 ubuntu14.04.2 LTS教程 http://jingyan.baidu.com/article/46650658228345f549e5f8cc.html ...

  7. poj2115 C Looooops

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29262   Accepted: 8441 Descr ...

  8. opencv函数制作的秒针模型

    曾经做过,没想到这次再次写这篇代码却用了这么久的时间.这回我要记住他. #include"cv.h" #include"highgui.h" int main( ...

  9. ACE服务端编程4:ACE跨平台之运行时初始化和关闭

    参考APG里的说法:平台差异及不兼容性的一个特别的方面,是对象的运行时初始化和程序关闭时这些对象的相应析构. ACE为了明确管理对象的清理,定义了ACE_Object_Manager类,这个类不仅涉及 ...

  10. 出现错误日志:The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path

      tomcat6出现错误日志: 信息: The APR based Apache Tomcat Native library which allows optimal performance in  ...