Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example 1:

Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.

Example 2:

Input: "cbbd"
Output: "bb"

题意:

最长回文子串

Solution1:  Brute Force. We can run three loops, the outer two loops pick all substrings one by one by locking the corner characters, the inner loop checks whether the picked substring is palindrome or not.

code:

 /*
Time complexity: O ( n^3 ) outer->2 for loops to find all possible substrings;
inner->1 while loop to check current substring isValidPalindrome
Space complexity: O ( 1 )
*/ class Solution {
public String longestPalindrome(String s) {
String res = "";
for (int i = 0; i < s.length(); i++) {
for (int j = i ; j < s.length(); j++) {
String sub = s.substring(i, j + 1);
if (isValidPalindrome(sub) && sub.length() > res.length()) {
res = sub;
}
}
}
return res;
} public boolean isValidPalindrome(String s){
int l = 0;
int r = s.length() - 1;
while (l < r){
if(s.charAt(l) != s.charAt(r)){
return false;
}else{
l++;
r--;
}
}
return true;
}
}

Solution2:  DP.

step1, initialize a matrix for saving intermediate info

step2, we can pre-calculate some info(此题代码可以将预处理合并到general case中来写)

step3, To fill the matrix.  If  char at start != char at end,  then s.substring[start, end] cannot be a palindrom, fill 'F' in such spot

step4, To fill the matrix.  If  char at start == char at end, it means two sides are the same. Then if we can make sure substring [start + 1 to end - 1] is panlindrom,  the whole substring should be a panlindrom.

step5, to fill the matrix in the same way

step6, update the longest result

code

 class Solution {
public String longestPalindrome(String s) {
String res = "";
boolean[][] dp = new boolean[s.length()][s.length()];
int max = 0;
for(int j= 0; j < s.length(); j++){
for(int i = 0; i<=j; i++){
dp[i][j] = s.charAt(i) == s.charAt(j) && ((j-i<=2)||dp[i+1][j-1]);
if(dp[i][j] && (j-i+1>max)){
max = j- i + 1;
res = s.substring(i,j+1);
}
}
}
return res;
}
}

[leetcode]5. Longest Palindromic Substring最长回文子串的更多相关文章

  1. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

  2. [LeetCode] 5. Longest Palindromic Substring 最长回文子串

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

  3. 【LeetCode】5. Longest Palindromic Substring 最长回文子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...

  4. LeetCode:Longest Palindromic Substring 最长回文子串

    题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  5. lintcode :Longest Palindromic Substring 最长回文子串

    题目 最长回文子串 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串. 样例 给出字符串 "abcdzdcab",它的最长回文 ...

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

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

  7. 1. Longest Palindromic Substring ( 最长回文子串 )

    要求: Given a string S, find the longest palindromic substring in S. (从字符串 S 中最长回文子字符串.) 何为回文字符串? A pa ...

  8. 【翻译】Longest Palindromic Substring 最长回文子串

    原文地址: http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-i.html 转载请注明出处:http:// ...

  9. 005 Longest Palindromic Substring 最长回文子串

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

随机推荐

  1. 第二章 JavaScript案例(中)

    1. js事件 HTML代码 <!DOCTYPE html> <html lang="en" onUnload="ud()"> < ...

  2. hadoop完全分步式搭建

    实验环境介绍 4台机器,规划如下: 计算机名 IP地址 角色 master 192.168.138.200 NameNode,SecondaryNameNode,ResourceManager sla ...

  3. docker push到私有仓库

    1.登录 docker login http://xxxxx.com 2.登录私有hub创建项目 例如项目叫:abc-dev 2.给镜像打tag docker tag 2e25d8496557 xxx ...

  4. sequelize查询数据的日期格式化

    首先确定时区 const sequelize = new Sequelize(config.database, config.username, config.password, { host: co ...

  5. 【oracle常见错误】oracle监听程序配置/“ORA-12541: TNS: 无监听程序”

    问题描述 在用PL/SQL Developer连接Oracle 11g时报错“ORA-12541: TNS: 无监听程序”,如下图所示.可以按照如下的步骤进行解决. 解决方案 监听程序配置 从开始菜单 ...

  6. 使用gitbook plugin

    使用gitbook plugin. { "title": "xx doc", "author": "morya", &q ...

  7. 通过eclipse打开jdk native源码

    1.下载 eclipse http://www.eclipse.org/downloads/eclipse-packages/ 建议下载 Eclipse IDE for Eclipse Committ ...

  8. linux服务之apache(二)

    1.ip/pv/uv(用来统计网站被访问情况) ip:表示该网站一天被多少ip访问过,一天一个ip之算做一次. pv:表示页面被访问的次数 uv:独立访客,一个用户就是一个uv. 2.创建虚拟主机 利 ...

  9. react学习笔记(二)

    在以类继承的方式定义的组件中,为了能方便地调用当前组件的其他成员方法或属性(如:this.state),通常需要将事件处理函数运行时的 this 指向当前组件实例. 绑定事件处理函数this的几种方法 ...

  10. linux 配置vue环境

    系统 [root@Gao conf.d]# uname -a 工具 1.Final Shell 2.工具截图 需要下载的部分 node.js    npm   cnpm   vue-cli 安装nod ...