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. vector 中需要注意的东西!

    vector的erase方法注意点!!! C++11是这样的: iterator erase (const_iterator position); iterator erase (const_iter ...

  2. Searching the Web论文阅读

    Searching the Web   (Arvind Arasu etc.) 1. 概述 2000年,23%网页每天更新,.com域内网页40%每天更新.网页生存半衰期是10天.描述方法可用Pois ...

  3. KEIL的多工程多目标

    https://blog.csdn.net/ybhuangfugui/article/details/51655502 https://mp.weixin.qq.com/s/CSUa4zegzz8JW ...

  4. maya 安装不上

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  5. [转]js判断url是否有效

    本文转自:http://www.cnblogs.com/fumj/p/3490121.html 方法一:(仅适用于ie) function CheckStatus(url) { XMLHTTP = n ...

  6. [Java][Servlet] Failed to destroy end point associated with ProtocolHandler ["http-nio-8080"]

    Background: Servlet version 3.1(3.0之后就有了@WebServlet注解) Error 严重: Failed to destroy end point associa ...

  7. springboot从入门到精通(一)

    springboot到底有什么好处?有什么优势?这个先不用看,我们只要知道它有很多优势,现在要做的事只有一件,那就是撸代码!撸完就知道有多少料! 首先,在案例中,我们会构建一个英雄列表应用.操作如下: ...

  8. MySQL中报错: [Err] 1146 - Table 'performance_schema.session_status' doesn't exist 解决办法

    解决办法:1.打开cmd 执行命令cd/ 进入C盘根目录2.dir 查看C盘根目录下文件夹  找到 Program Files文件夹3.cd Program Files 进入该文件夹下 再输入dir ...

  9. Spring Data JPA 事务

    Spring Data JPA 是 Spring 基于 ORM 框架.JPA 规范的基础上封装的一套 JPA 应用框架,可使开发者用极简的代码即可实现对数据的访问和操作.它提供了包括增删改查等在内的常 ...

  10. jsp---tomcat===》》内置对象

    1.内置对象:    request: 方法:getParameter("txtName"):获取和页面上的name属性对象的value值       返回String       ...