*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 ...
随机推荐
- Go语言基础练习题系列2
1.练习1 生成一个随机数,让一个用户去猜这个数是多少? 代码示例如下: package main import ( "fmt" "math/rand" //m ...
- 2.Spring的Bean生命周期和组装方式
1.Spring IoC容器概述 Spring IoC容器: Spring容器即体现了IoC原理 Spring容器通过读取配置元数据负责对Beans实例化.配置和装配 配置元数据可以用X ...
- 读书笔记 - 《黑旗 ISIS的崛起》
不愧是普利策奖的书籍,读起来让人欲罢不能,花了大约四个晚上把此书一气读完.这本书讲述的是“伊斯兰国”ISIS及其前身组织历史,也就是阿布·穆萨卡·扎卡维及其追随者的故事.虽然不是小说,但故事的精彩以及 ...
- Launch iCar Scan Android Scanner Support Bluetooth X431 iDiag Update Version
Autonumen.com release new Launch iCar Scan for Android,Launch iCarScan Bluetooth Scanner is update v ...
- 13-----BBS论坛
BBS论坛(十三) 13.1点击更换图形验证码 (1)front/signup.html <div class="form-group"> <div class= ...
- 使用 Fetch完成AJAX请求
使用 Fetch完成AJAX请求 写在前面 无论用JavaScript发送或获取信息,我们都会用到Ajax.Ajax不需要刷新页面就能发送和获取信息,能使网页实现异步更新. 几年前,初始化Ajax一般 ...
- 关于django2.0的外键关系新特性之on_delete!
Django2.0里model外键和一对一的on_delete参数 在django2.0后,定义外键和一对一关系的时候需要加on_delete选项,此参数为了避免两个表里的数据不一致问题,不然会报错: ...
- MemoryFile匿名共享内存
Android提供了一个高效的共享内存机制.如果应用中涉及到在多个进程间交换数据时使用Android提高的共享内存机制将会大大的提高效率.但是也许是出于安全考虑,在应用层使用共享内存机制将会遇到很多障 ...
- POI 按word模版生成合同并生成PDF
功能需求:根据用户给的word版本合同文件.docx,实现模版替换功能. 如: 功能:支持段落和表格里的文字替换,还可以支持表格替换.如果需要段落需要换行用<br>隔开如:身份证<b ...
- 性能测试工具LoadRunner15-LR之负载生成器(Load Generators)
简介 对场景进行设计后,需要对负载生成器进行管理和配置.Load Generators是运行脚本的负载引擎(相当于加压机)主要功能是生成虚拟用户进行负载,在默认情况下使用本地的负载生成器来运行脚本. ...