*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 ...
随机推荐
- lintcode - 房屋染色
class Solution { public: /* * @param costs: n x 3 cost matrix * @return: An integer, the minimum cos ...
- java读取配置到Hash表里
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; im ...
- css animation fade in
<html> <style> @-webkit-keyframes fadeIn{0%{opacity:0}100%{opacity:1}} @-o-keyframes fad ...
- java——巨简陋文本编辑器
String :equals()方法是进行内容比较,而不是引用比较. “==”比较两个变量本身的值,即两个对象在内存中的首地址. Scanner :用Scanner实现字符串的输入有两种方法,一种是n ...
- ORACLE CBC LATCH 检查
###############1.DB meet latch: cache buffers chains event from awr report ,check latch: cache buffe ...
- accept 和 connect API深入 重点accept阻塞和非阻塞问题学习
https://www.cnblogs.com/zhangkele/p/10284234.html
- 跟我一起用python画你所想吧!
0.库的引入 要想画图,我们先倒入两个库. import numpy as np import matplotlib.pyplot as plt 注:以下代码全都基于导入这两个库的前提下编写的. 1. ...
- ubuntu下mysql安装(server、client、dev),开启、停止和重启,及常见错误
转自:ubuntu下mysql安装(server.client.dev),开启.停止和重启,及常见错误 1. 在ubuntu下安装server和client很简单: (1)安装server apt-g ...
- jQuery 3 有哪些新东西
jQuery 的横空出世,至今已有十个年头了,而它的长盛不衰显然不是没有理由的.jQuery 提供了极为友好的接口,使得开发者们可以方便地进行 DOM 操作.发起 Ajax 请求.生成动画……不一而足 ...
- C# 使用消息队列,包括远程访问
转:https://www.cnblogs.com/80X86/p/5557801.html 功能需求,用到了队列,用的时候出了很多问题,现在总结一下,希望能对有需要的人提供帮助. 我的需求很简单,就 ...