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"

solution: using dp: i : start index, j : ending index

given fixed size(number of subString), check each substring(from s)

class Solution {
public String longestPalindrome(String s) {
//nature structure
//given fixed step(number,size), check each subString
//dp -- from 0 to n-1
int max = 0;
String res = "";
int n = s.length();
boolean[][] dp = new boolean[n][n];
for(int i = 0; i<n;i++){//fixed number
for(int j = 0; j+i<n; j++){//start inex
if(s.charAt(j) == s.charAt(j+i)){
if(i<2 || dp[j+1][j+i-1]){ // 0 or 1
dp[j][j+i] = true;
dp[j+i][j] = true;
if(max<i+1){
max = i+1;
res = s.substring(j,j+i+1);
}
}
}
}
}
//System.out.println(max);
return res; }
}

more solution here

https://leetcode.com/problems/longest-palindromic-substring/solution/

*5. Longest Palindromic Substring (dp) previous blogs are helpful的更多相关文章

  1. 最长回文子串(Longest Palindromic Substring)-DP问题

    问题描述: 给定一个字符串S,找出它的最大的回文子串,你可以假设字符串的最大长度是1000,而且存在唯一的最长回文子串 . 思路分析: 动态规划的思路:dp[i][j] 表示的是 从i 到 j 的字串 ...

  2. Leetcode:【DP】Longest Palindromic Substring 解题报告

    Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...

  3. 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 ...

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

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

  5. 5. Longest Palindromic Substring (DP)

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

  6. 5. Longest Palindromic Substring - Unsolved

    https://leetcode.com/problems/longest-palindromic-substring/#/description Given a string s, find the ...

  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 ...

随机推荐

  1. BestCoder Round #64 1001

    Numbers  Accepts: 480  Submissions: 1518  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 6553 ...

  2. java拦截器的使用

    转载: https://www.cnblogs.com/liangblog/p/7234757.html https://blog.csdn.net/reggergdsg/article/detail ...

  3. 普通用户不能使用sudo命令的解决办法

    普通用户不能使用sudo命令的解决办法 https://www.cnblogs.com/fasthorse/p/5949946.html 1. 切换到root用户下:su – root 2. 给/et ...

  4. Python开源库

    某些情况下,pip install xxx找不到,而且在 官方库 也找不到. 那么 第三方库 就派上用场了.

  5. 剑指offer中经典的算法题之从头到尾打印链表

    话不多说上代码: 我自己的算法是: /** * public class ListNode { * int val; * ListNode next = null; * * ListNode(int ...

  6. (转)shell中各种括号的作用()、(())、[]、[[]]、{}

    shell中各种括号的作用().(()).[].[[]].{} 原文:http://www.jb51.net/article/60326.htm http://blog.csdn.net/good_h ...

  7. contiki源码阅读之list

    我们阅读一下contiki的源码,list.c(路径是./core/lib/list.h). #include "lib/list.h" #define NULL 0 struct ...

  8. plsql窗口列表保持

    使用plsql窗口列表保持方法如下: 1.工具——首选项——用户界面——自动保存桌面 选一下此项就保存你当前的窗口布局了,下次启动就不用再设置了. 英语版自己对照.

  9. js对象动态赋值

    <view class="movies-template"> <template is="movieListTemplate" data=&q ...

  10. #与javascript:void(0)的区别

    #"包含了一个位置信息 默认的锚点是#top 也就是网页的上端 而javascript:void(0)  仅仅表示一个死链接 这就是为什么有的时候页面很长浏览链接明明是#可是跳动到了页首 而 ...