题目链接: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. Python中int()函数的用法浅析

      int()是Python的一个内部函数 Python系统帮助里面是这么说的 >>> help(int)  Help on class int in module __builti ...

  2. day4 列表 增删改查 元组

    增lis=["a","b","c",5,7,4]lis.append("s")#在列表的末尾追加lis.extend(& ...

  3. liunx 用户和用户组的命令

    查看用户列表     /etc/passwd  查看用户组列表 /etc/group 用户和用户组管理 groupadd groups 新增用户组 groupmod -n newgroupsr gro ...

  4. IOS中手势UIGestureRecognizer

    通常在对视图进行缩放移动等操作的时候我们可以用UIScrollView,因为它里边自带了这些功能,我们要做的就是告诉UIScrollView的几个相关参数就可以了 但是没有实现旋转的手势即UIRota ...

  5. 【BZOJ4300】绝世好题(动态规划)

    [BZOJ4300]绝世好题(动态规划) 题面 BZOJ Description 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<=l ...

  6. open_basedir restriction in effect的错误及其解决办法

    问题是出现在了PHP.INI上面了   原因是php.ini里设置了     opendir=/var/web/w0895/:/tmp:/usr/lib/php  解答: 其实open_basedir ...

  7. Hive(二)hive的基本操作

    一.DDL操作(定义操作) 1.创建表 (1)建表语法结构 CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name[(col_name data_type ...

  8. opencv查找轮廓---cvFindContours && cvDrawCountours 用法及例子

    http://blog.csdn.net/timidsmile/article/details/8519751 环境: vs2008 + opencv2.1 先看,这两个函数的用法(参考 opencv ...

  9. C/C++中如何计算程序运行的时间

    一个程序的功能通常有很多种方法来实现,怎么样的程序才算得上最优呢?举个例子,如果实现同一个功能的两个程序,一个一点按钮就给出运行结果,而另一个则需要漫长的时间去等待,就像安装WINDOWS XP一样( ...

  10. EA画时序图初试

    1.步骤: 1. 新建一个项目: 2. Use Case Model右键-->添加图-->左边选择UML Behavioral,右边选择Sequence: 3. 选择工具栏中的工具,点击工 ...