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)级别的时间 ...
随机推荐
- Django:CSRF(Cross-request forgery)跨站请求伪造
一.CSRF是什么 二.CSRF攻击原理 三.CSRF攻击防范 一.CSRF是什么 CSRF(Cross-site request forgery)跨站请求伪造,也被称为“One Click Atta ...
- BIOS+MBR操作系统引导方式
1. 主引导记录(Master Boot Record,缩写:MBR) 主引导记录又叫做主引导扇区,是计算机开机后启动操作系统时所必须要读取的硬盘首个扇区,它在硬盘上的三维地址为(柱面,磁头,扇区)= ...
- cuda环境搭建
cuda环境搭建 cuda 的安装 一篇很不错的博客 https://blog.csdn.net/u014529295/article/details/78766258 另外官网也有介绍 https: ...
- 接口调优——WebAPI 过滤器,IIS WebDAV
目录 1.身份认证过滤器—AuthenticationFilter 2.Action 过滤器—ActionFilter 3.异常处理过滤器—ExceptionFilterAttribute 4.IS ...
- Java8新特性--CompletableFuture
并发与并行 Java 5并发库主要关注于异步任务的处理,它采用了这样一种模式,producer线程创建任务并且利用阻塞队列将其传递给任务的consumer.这种模型在Java 7和8中进一步发展,并且 ...
- 2019-2020-1 20199312《Linux内核原理与分析》第一周作业
实验一:linux系统简介 Linux 本身只是操作系统的内核.内核是使其它程序能够运行的基础.它实现了多任务和硬件管理,用户或者系统管理员交互运行的所有程序实际上都运行在内核之上.其中有些程序是必需 ...
- Educational Codeforces Round 75 (Rated for Div. 2) C. Minimize The Integer
链接: https://codeforces.com/contest/1251/problem/C 题意: You are given a huge integer a consisting of n ...
- TDOA 之TDOA算法python实现
这里指的TDOA算法,实际是解两个双曲线方程,由于两个二次方程设计东西较多,如果强解,计算量很大,从网上参考了如下链接: 算法推到:https://blog.csdn.net/lpsl1882/art ...
- emit传多个参数
https://blog.csdn.net/lxy123456780/article/details/87811113 子组件: this.$emit('closeChange',false,true ...
- php数据类型之 NULL类型
空在英文里面表示是null,它是代表没有.空(null)不是false,不是0,也不是空格. [重点]知道null产生的三种情况,学习empty 和 isset两个函数的区别.大理石平台怎么样 主要有 ...