Leetcode: Longest Palindromic Substring && Summary: Palindrome
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"
Analysis: 想到了Naive的方法,时间复杂度为O(N^3),知道肯定不是最优的方法,一时也想不出别的方法,于是看了网上的做法,最后采纳了这个人的方法,感觉他讲的比较全面:http://www.programcreek.com/2013/12/leetcode-solution-of-longest-palindromic-substring-java/
1. Naive Approach: The time complexity is O(n^3). If this is submitted to LeetCode onlinejudge, "Time Limit Exceeded".
2. Dynamic Programming:
Let s be the input string, i and j are two indices of the string.
Define a 2-dimension array "table" and let table[i][j] denote whether substring from i to j is palindrome.
Start condition:
table[i][i] == 1;
table[i][i+1] == 1 => s.charAt(i) == s.charAt(i+1)
Changing condition:
table[i][j] == 1 => table[i+1][j-1] == 1 && s.charAt(i) == s.charAt(j)
Time O(n^2) Space O(n^2)
以下代码参考了Code Ganker的,这种方法使用两层循环,时间复杂度是O(n^2)
public String longestPalindrome(String s) {
if(s == null || s.length()==0)
return "";
boolean[][] palin = new boolean[s.length()][s.length()];
String res = "";
int maxLen = 0;
for(int i=s.length()-1;i>=0;i--)
{
for(int j=i;j<s.length();j++)
{
if(s.charAt(i)==s.charAt(j) && (j-i<=2 || palin[i+1][j-1]))
{
palin[i][j] = true;
if(maxLen<j-i+1)
{
maxLen=j-i+1;
res = s.substring(i,j+1);
}
}
}
}
return res;
}
2. Further Optimize:我自己的进一步考虑,空间可以进一步优化从O(n^2) -> O(n), 因为我们并不需要存储从任意点开始子字符串的回文情况,例如计算从i开始的回文,我们只需要从i+1开始的回文信息,而不需要从i+2,i+3,...等等节点开始的回文信息,因此只需要一个一位数组记录i+1开始的情况,这样二维数组变一维
3. 中心扩散法 Spread From Center
复杂度
时间 O(n^2) 空间 O(1)
思路
动态规划虽然优化了时间,但也浪费了空间。实际上我们并不需要一直存储所有子字符串的回文情况,我们需要知道的只是中心对称的较小一层是否是回文。所以如果我们从小到大连续以某点为个中心的所有子字符串进行计算,就能省略这个空间。 这种解法中,外层循环遍历的是子字符串的中心点,内层循环则是从中心扩散,一旦不是回文就不再计算其他以此为中心的较大的字符串。由于中心对称有两种情况,一是奇数个字母以某个字母对称,而是偶数个字母以两个字母中间为对称,所以我们要分别计算这两种对称情况。
class Solution {
int lo, maxLen; public String longestPalindrome(String s) {
if (s == null || s.length() == 0) return ""; for (int i = 0; i < s.length(); i ++) {
findPalindrome(s, i, i); //assume odd length, try to extend Palindrome as possible
findPalindrome(s, i, i + 1); //assume even length.
} return s.substring(lo, lo + maxLen);
} public void findPalindrome(String s, int l, int h) {
while (l >= 0 && h < s.length() && s.charAt(l) == s.charAt(h)) {
if (h - l + 1 > maxLen) {
maxLen = h - l + 1;
lo = l;
}
l --;
h ++;
}
}
}
O(N)的方法有Manacher's Algorithm, 比较复杂,有时间再去搞懂吧: https://www.felix021.com/blog/read.php?2040
Leetcode: Longest Palindromic Substring && Summary: Palindrome的更多相关文章
- [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 ...
- [LeetCode] Longest Palindromic Substring(manacher algorithm)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- C++ leetcode Longest Palindromic Substring
明天就要上课了,再过几天又要见班主任汇报项目进程了,什么都没做的我竟然有一种迷之淡定,大概是想体验一波熬夜修仙的快乐了.不管怎么说,每天还是要水一篇博文,写一个LeetCode的题才圆满. 题目:Gi ...
- LeetCode:Longest Palindromic Substring 最长回文子串
题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- Leetcode: Longest Palindromic Substring. java
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 ...
- [LeetCode]Longest Palindromic Substring题解(动态规划)
Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...
- Leetcode:Longest Palindromic Substring分析和实现
问题大意是在给定字符串中查找最长的回文子串,所谓的回文就是依据中间位置对称的字符串,比如abba,aba都是回文. 这个问题初一看,非常简单,但是会很快发现那些简单的思路都会带来O(n^3)级别的时间 ...
随机推荐
- SQL注入是什么?如何防止?
SQL注入是什么?如何防止? SQL注入是一种注入攻击,可以执行恶意SQL语句.下面本篇文章就来带大家了解一下SQL注入,简单介绍一下防止SQL注入攻击的方法,希望对大家有所帮助. 什么是SQL注入? ...
- 搭建jumpser堡垒机
安装环境centos7 生产环境建议使用 1.4.8 版本 1.开始安装 echo -e "\033[31m 1. 防火墙 Selinux 设置 \033[0m" \ && ...
- Python基础——运行机理
Python解释器 从Python这门编程语言的实现上来讲,Python是一个名为解释器的软件包,包含:一个解释器 和 支持的库 解释器又有不同版本:CPython.Jython.IronPython ...
- Springboot分别使用乐观锁和分布式锁(基于redisson)完成高并发防超卖
原文 :https://blog.csdn.net/tianyaleixiaowu/article/details/90036180 乐观锁 乐观锁就是在修改时,带上version版本号.这样如果试图 ...
- 【BZOJ3691】游行 最小可相交路径覆盖转化
因为C是不断变化的而且C是和点权相关和边权无关 所以我们可以MCMF但是MCMF的时候不能与C相关 再分析问题 我们可以认为每条路径S->T只覆盖T这个终点 因为题目中说了如果Si != Ti ...
- TeX Live安装指南
若要安装 TeXLive ,推荐下载 TeXLive 的 ISO 镜像,因为在线安装下载过程会很慢.如果网速快也可以在线安装.下载地址:http://mirror.ctan.org/systems/t ...
- 洛谷P1050 循环【java大数】
题目:https://www.luogu.org/problemnew/show/P1050 题意:给定一个数$n$,问$n$的幂次的最低$k$位的循环节是多少. 思路:这真是我做过最难的java大数 ...
- vue 安装 ‘node-sass’ 运行报错:ERROR in Cannot find module 'node-sass'
好像是由于cnpm安装导致.执行下面的安装代码,或者使用npm 安装node-sass cnpm install node-sass@latest
- Windows10下安装Ubuntu18.04LTS详细教程
这篇文章分享自己在Windows10系统下安装VMware虚拟机,然后在VMware中安装Ubuntu 18.04 LTS的详细过程.之所以选择在虚拟机中安装Ubuntu,主要是可以不影响自己电脑的正 ...
- git 常用命令使用,git bash通用命令
git 常用命令 1.强制推送(慎用,除非你认为其他冲突等可以丢弃 或者不是很重要) git push -- force 2.创建文件等小命令 touch a // 创建一个a文件 >> ...