题目链接: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. Strus默认跳转方式是请求转发 地址栏不变 与javaweb的内部转发一样

    Strus默认跳转方式是请求转发 地址栏不变 与javaweb的内部转发一样

  2. 【bzoj5206】[Jsoi2017]原力 根号分治+STL-map

    题目描述 一个原力网络可以看成是一个可能存在重边但没有自环的无向图.每条边有一种属性和一个权值.属性可能是R.G.B三种当中的一种,代表这条边上原力的类型.权值是一个正整数,代表这条边上的原力强度.原 ...

  3. 【比赛】HNOI2018 游戏

    考试的时候线段树区间查询的return条件打成了l==r....于是光荣爆20(线段树都不会打了?) 看膜博士的题解 #include<bits/stdc++.h> #define ui ...

  4. BZOJ 1367 [Baltic2004]sequence 解题报告

    BZOJ 1367 [Baltic2004]sequence Description 给定一个序列\(t_1,t_2,\dots,t_N\),求一个递增序列\(z_1<z_2<\dots& ...

  5. word----遇到问题-----word中插入的图片无法左对齐----格式按钮为灰色

    当我们在用word时,有时要插入图片,却发现,插入的图片只在中间位置,不能拖到左边,这时怎么办呢 主要是图层的高低原因导致的不能拖动. 这个时候我们只需要设置一下图片的图层类型即可. 对着图片右键在设 ...

  6. MapReduce(五) mapreduce的shuffle机制 与 Yarn

    一.shuffle机制 1.概述 (1)MapReduce 中, map 阶段处理的数据如何传递给 reduce 阶段,是 MapReduce 框架中最关键的一个流程,这个流程就叫 Shuffle:( ...

  7. 51nod 1952 栈(单调队列)

    用deque实时维护栈的情况. 数加入栈顶部,删掉栈顶部的数,相当于加入一个数,删掉最早出现的数,每次求最大值,这个直接记录一下就好了. 数加入栈底部,删掉栈顶部的数,相当于加入一个数,删掉最晚出现的 ...

  8. 【bzoj3796】Mushroom追妹纸

    Portal -->bzoj3796 Description 给出字符串s1.s2.s3,找出一个字符串w,满足: 1.w是s1的子串: 2.w是s2的子串: 3.s3不是w的子串. ​ 求w的 ...

  9. php网摘收藏

    1.thinkphp3.2.3开发手册: http://document.thinkphp.cn/manual_3_2.html 2.ThinkPHP3.2.3的函数汇总:http://www.thi ...

  10. Python入门记录

    最近看到Python3.7版本已经发布了,安装了Aconda最新的版本.安装完成后测试: 在Python程序里有两种办法查看Python版本信息: import sys # 查看版本 print(sy ...