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)级别的时间 ...
随机推荐
- git命令——revert、reset
参考:如何在 Git 中重置.恢复,返回到以前的状态 使用git时,如果对刚刚提交的后悔了怎么办,如何撤销? 方法一:手动修改 你把新增的文件删了 或者 更改过的文件再改回来,然后再commit一次. ...
- AttributeError: 'NoneType' object has no attribute 'extend'
Python使用中可能遇到的小问题 AttributeError: 'NoneType' object has no attribute 'extend' 或者AttributeError: 'Non ...
- 三维视觉、SLAM方向全球顶尖实验室汇总
本文作者 任旭倩,公众号:计算机视觉life,编辑成员 欧洲 英国伦敦大学帝国理工学院 Dyson 机器人实验室 http://www.imperial.ac.uk/dyson-robotics-la ...
- 牛客练习赛48 C 小w的糖果 (数学,多项式,差分)
牛客练习赛48 C 小w的糖果 (数学,多项式) 链接:https://ac.nowcoder.com/acm/contest/923/C来源:牛客网 题目描述 小w和他的两位队友teito.toki ...
- 前端笔记-bom
BOM对象 BOM即浏览器对象模型,它与dom不同的是可以操作浏览器窗口,使用它的接口我们可以改变窗口,状态栏,文本,及其他与除页面以外其他动作,使得js可以和我们浏览器进行沟通 窗口 即window ...
- 20 区分webpack中导入vue和普通网页使用script导入Vue的区别
回顾包的查找规则: 1.找项目根目录中有没有node_modules的文件夹 2.在node_modules中根据包名,找对应的vue文件夹 3.在vue文件夹中,找一个叫做package.json的 ...
- 网站添加logo图片
网站添加log图片 第一种方法 这里使用的图片一般为16*16大小的图片 <link rel="shortcut icon" href="http://xxx.xx ...
- 微信小程序导入Vant报错
作者:如也_d1c0链接:https://www.jianshu.com/p/0d2332984f8c来源:简书简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处. 先放出来Vant ...
- 2019HDU多校第九场 Rikka with Quicksort —— 数学推导&&分段打表
题意 设 $$g_m(n)=\begin{cases}& g_m(i) = 0, \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ...
- BZOJ4543 Hotel加强版(长链剖分)
题意 求一棵树上,两两距离相等的三个点的三元组(无序)的个数. 题解 转自 CaptainHarryChen 的博客 CODE 代码中的f,gf,gf,g对应题解中的num,waynum,waynum ...