题目链接: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. hdu6415 Rikka with Nash Equilibrium (DP)

    题目链接 Problem Description Nash Equilibrium is an important concept in game theory. Rikka and Yuta are ...

  2. appium手机操作

    1.按键操作 pressKeyCode(key, metastate) key为按键事件,metastate为辅助功能键 举例: pressKeyCode(AndroidKeyCode.HOME)   ...

  3. BZOJ3566 SHOI2014概率充电器(动态规划+概率期望)

    设f[i]为i在子树内不与充电点连通的概率.则f[i]=(1-pi)·∏(1-qk+qk·f[k]). 然后从父亲更新答案.则f[i]=f[i]·(1-qfa+qfa*f[fa]/(1-qfa+qfa ...

  4. window上安装elasticserach

    提供一个百度云链接下载elasticsearch (链接:https://pan.baidu.com/s/1sk8PYjV 密码:l586) 测试达到目的:安装elasticsearch后再安装hea ...

  5. bzoj 1004 组合

    代码: //根据Burnside定理:有m个置换k钟颜色,所有本质不同的染色方案数就是每种置换的不变元素的个数的平均数.所谓不变元素就是一种染色方案 //经过置换变换后和之前一样.所以现在就是要求不变 ...

  6. 使用jconsole工具来监控java运行情况

    参考:http://blog.163.com/lucas_nina/blog/static/185960149201493034258448/   经验证OK   jconsole是jdk自带的工具. ...

  7. 用for语句从数组中剔除数据,注意,count,要放到for语句之外才行

    date_default_timezone_set('Asia/Shanghai'); $arr = array( ,), ,), ,), ,) ); print_r($arr); ;$i<co ...

  8. Java的三大特性之封装

    java提高篇(一)-----理解java的三大特性之封装 三大特性之---封装 封装从字面上来理解就是包装的意思,专业点就是信息隐藏,是指利用抽象数据类型将数据和基于数据的操作封装在一起,使其构成一 ...

  9. 将本地项目和远程git仓库相连接

    有时候写代码,是存在本地的,远程仓库没有对应的代码库,这个时候就需要把本地的代码项目与远程git库连接并推送. 1. 将项目文件添加到仓库中 本地的项目文档: 添加项目文件 git add . 2. ...

  10. poj 3164 Command Network(最小树形图模板)

    Command Network http://poj.org/problem?id=3164 Time Limit: 1000MS   Memory Limit: 131072K Total Subm ...