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. The Twelve-Factor Container

    转自:https://medium.com/notbinary/the-twelve-factor-container-8d1edc2a49d4?%24identity_id=550978996201 ...

  2. oracle-企业信息化

    http://www.itpub.net/thread-1873735-1-1.html        OCP考试心得 http://blog.csdn.net/rlhua/article/detai ...

  3. jmeter 关联

    (1)正则表达式提取器 注:正则表达式部分配置说明 案例: 动态值:name=userSession value=119828.306682868zVzDzzipfcfDzQicpAVVVf> ...

  4. 滑雪(dp)

    问题 H: [例9.24]滑雪 时间限制: 1 Sec  内存限制: 128 MB提交: 21  解决: 13 题目描述 小明喜欢滑雪,因为滑雪的确很刺激,可是为了获得速度,滑的区域必须向下倾斜,当小 ...

  5. windows配置远程桌面连接到ubuntu

    最近在用nodejs开发项目,同时也在做一些区块链相关的工作,公司给配的办公电脑着实不错,都是自家品牌的工作站,市场价都是15K+了.但是在win10上装虚拟机,还是不太顺畅的.因为公司电脑是五年到期 ...

  6. 内训--PPT演示技巧

    企业内训,实操,不涉及如何做PPT,仅讲解PPT播放等内容.做好PPT后,在演讲的过程中,如何使用这些功能.

  7. 换上 SansForgetica-Regular 字体,增加记忆能力

    最近澳大利亚的RMIT(皇家墨尔本理工大学) 搞出来这么个字体,号称能增强记忆,原理是通过难以识别的字体,让人提起精神去识别,从而记忆更深刻. 果断弄了个试试. 安装过程: 下载字体文件 点这里去下载 ...

  8. C# 调用Tesseract实现OCR

    介绍 Tesseract是一个基于Apache2.0协议开源的跨平台ocr引擎,支持多种语言的识别,在Windows和Linux上都有良好的支持. 创建工程 创建一个C#的控制台工程 添加System ...

  9. FPGA editor 的使用之一 ------ Probe探针

    做FPGA设计经常会用到FPGA editor工具,今天开始总结下使用FPGA editor工具的经验. 1.添加probes 在debug时,经常要分析设计中的某一个信号的状态变化,需要观测这个信号 ...

  10. vue+窗格切换+田字+dicom显示_01

    环境:vue+webpack+cornerstone ide:vs code 需求:窗格设置+拼图设置 1.点击左边第一个窗格或者默认显示. 2.点击第二个也同理显示,以此类推 3.选择左边的窗格之后 ...