*5. Longest Palindromic Substring (dp) previous blogs are helpful
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的更多相关文章
- 最长回文子串(Longest Palindromic Substring)-DP问题
问题描述: 给定一个字符串S,找出它的最大的回文子串,你可以假设字符串的最大长度是1000,而且存在唯一的最长回文子串 . 思路分析: 动态规划的思路:dp[i][j] 表示的是 从i 到 j 的字串 ...
- Leetcode:【DP】Longest Palindromic Substring 解题报告
Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...
- 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 ...
- 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. Longest Palindromic Substring (DP)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 5. Longest Palindromic Substring - Unsolved
https://leetcode.com/problems/longest-palindromic-substring/#/description Given a string s, find the ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- Leetcode Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【leedcode】 Longest Palindromic Substring
Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...
随机推荐
- 2015苏州大学ACM-ICPC集训队选拔赛(1) 1007
连通图 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submissio ...
- Linux UDEV提权过程
1.下载攻击脚本 [test@H0f ~]$ wget http://www.extmail .org/source/exploit-udev-8478 --2018-04-02 01:21:00-- ...
- c++中代理类的学习
https://blog.csdn.net/lcg910978041/article/details/51468680 C++代理类是为了解决这样的问题: 容器通常只能包含一种类型的对象,所以很难在容 ...
- 3DMAx Panda Directx Exporter 导出 X插件
Panda Directx Exporter 下载地址 http://www.andytather.co.uk/Panda/directxmax_downloads.aspx 将下载的文件解压后,放到 ...
- python_文件 处理
一.字符编码 内存固定使用unicode编码 数据最先产生于内存中,是unicode格式,要想传输需要转成bytes格式 # unicode -------> enconde( u t f - ...
- android Activity启动过程(一)从startActivty开始说起
从启动startActivity开始说起 MainActivity.startActivity() Activity.startActivity() Activity.startActivityFor ...
- CAD安装失败怎样卸载CAD 2012?错误提示某些产品无法安装
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- day06 - Python - 面向对象
本节内容: 引子 面向对象 v.s. 面向过程 面向对象编程介绍 面向对象的特性: 封装 继承 多态 类.方法 1.引子 假设你现在是一家游戏公司的开发人员,现 ...
- Windows Server 2008下的FTP服务器设置
今天刚在新服务器上部署好Windows Server 2008,同时安装好了SQL,基本上还算顺利.没想到在设置FTP服务器的时候遇到了麻烦.按照以往的经验,安装好Serv-U以后,同时在防火墙设置里 ...
- Jersey初始化配置
一 实际项目配置 公司VIP平台因为业务的特殊性,对业务数据的操作.以及前后端解耦等要求,使用到了jersey框架.同时使用到了spring框架. 二 jersey初始化 配置web项目配置文件web ...