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. PythonStudy——函数的参数 Function argument

    形参与实参 # 参数介绍:# 函数为什么要有参数:因为内部的函数体需要外部的数据# 怎么定义函数的参数:在定义函数阶段,函数名后面()中来定义函数的参数# 怎么使用函数的参数:在函数体中用定义的参数名 ...

  2. 第十七章 java8特性

    17.java8中Lambda表达式与Stream API的使用 17.1 Lambda 表达式(Lambda Expressions) 1课时 17.2 函数式(Functional)接口 1课时 ...

  3. zabbix之 自定义(指定特定磁盘)监控io

    引言 zabbix自带的模板,并且完成了我们的一些比较常用的监控,现在我们如果想要监控我们磁盘的IO,这时候zabbix并没有给我们提供这么一个模板,所以我们需要自己来创建一个模板来完成磁盘IO的监控 ...

  4. 基于redis的 分布式锁 Java实现

    package com.hs.services.lock; import java.util.concurrent.TimeUnit; import javax.annotation.Resource ...

  5. MVC 中Scripts.Render、Styles.Render

    在ASP.NET MVC项目中,可以在视图中利用Scripts.Render.Styles.Render统一加载js.css文件,需要利用BundleConfig类来Add 各种Bundle,例如:b ...

  6. myibatis的坑--text类型的字段查询缺失

    问题:某个字段的类型为text(或者mediumtext,longtext)的时候,用selectByQuery语句查询出来的结果不包含该字段内容. myibatis 用mybatis-generat ...

  7. 关于APS在企业生产计划上的应用

    本人本身是一个码农,已经服务了共和国各项事业(好像是说得有点漂,没办法段子看多了)大约一半工作时候了(按60岁退休的话),从一线的小码农,到现在成了老农,出产了不少或优或劣的各种码,几乎啥都做过.近几 ...

  8. linux命令行命令

    Linux命令行编辑快捷键: history 显示命令历史列表 ↑(Ctrl+p) 显示上一条命令 ↓(Ctrl+n) 显示下一条命令 !num 执行命令历史列表的第num条命令 !! 执行上一条命令 ...

  9. 【剑指offer】数组中只出现一次的数字

    题目:一个整型数组里除了两个数字之外,其他的数字都出现了偶数次.请写程序找出这两个只出现一次的数字. 思路1:使用HashMap存上所有的数字,数字作为Key,Value为对应的出现次数.这种做法可以 ...

  10. 报错:NoSuchMethodError: kafka.javaapi.PartitionMetadata.leader()Lkafka/cluster/Broker;

    报错现象: 在pom文件添加: <dependency> <groupId>org.apache.kafka</groupId> <artifactId> ...