LeetCode--5. Longest Palindromic Substring
来源:互联网 发布:北京淘宝美工培训学校 编辑:程序博客网 时间:2024/05/16 14:08
Problem:
Given a string S, find the longest palindromic substring in S. You may
assume that the maximum length of S is 1000, and there exists one
unique longest palindromic substring.
Analysis:
这个题目有四种解答方法:
两侧比较法,时间复杂度O(n^3) ,空间复杂度O(1)
这个方法严重超时,一点优化都没有。就是进行群遍历
中心扩展法 ,时间复杂度O(n^2),空间复杂度O(1)
因为回文字符串是以中心轴对称的,所以如果我们从下标 i 出发,用2个指针向 i 的两边扩展判断是否相等,那么只需要对0到n-1的下标都做此操作,就可以求出最长的回文子串。但需要注意的是,回文字符串有奇偶对称之分,即”abcba”与”abba”2种类型,因此需要在代码编写时都做判断。
设函数int Palindromic ( string &s, int i ,int j) 是求由下标 i 和 j 向两边扩展的回文串的长度,那么对0至n-1的下标,调用2次此函数:
int lenOdd = Palindromic( str, i, i ) 和 int lenEven = Palindromic (str , i , j ),即可求得以i 下标为奇回文和偶回文的子串长度。接下来以lenOdd和lenEven中的最大值与当前最大值max比较即可。这个方法有一个好处是时间复杂度为O(n2),且不需要使用额外的空间动态规划,时间复杂度O(n^2),空间复杂度O(n^2)
假设dp[ i ][ j ]的值为true,表示字符串s中下标从 i 到 j 的字符组成的子串是回文串。那么可以推出:
dp[ i ][ j ] = dp[ i + 1][ j - 1] && s[ i ] == s[ j ]。
这是一般的情况,由于需要依靠i+1, j -1,所以有可能 i + 1 = j -1, i +1 = (j - 1) -1,因此需要求出基准情况才能套用以上的公式:
a. i + 1 = j -1,即回文长度为1时,dp[ i ][ i ] = true;
b. i +1 = (j - 1) -1,即回文长度为2时,dp[ i ][ i + 1] = (s[ i ] == s[ i + 1])。
有了以上分析就可以写出代码了。需要注意的是动态规划需要额外的O(n2)的空间。O(n) 马拉车(Manacher算法),时间复杂度O(n),空间复杂度O(n^2)
时间复杂度为什么是O(N)而不是O(N²)呢?
时间复杂度为什么是O(N)?
假设真的是O(N²),那么在每次外层的for循环进行的时候(一共n步),对于for的每一步,内层的while循环要进行O(N)次。而这是不可能。因为p[i]和R是有相互影响的。while要么就只走一步,就到了退出条件了。要么就走很多很步。如果while走了很多步,多到一定程度,会更新R的值,使得R的值增大。而一旦R变大了,下一次进行for循环的时候,while条件直接就退出了。
附:
解法: http://articles.leetcode.com/longest-palindromic-substring-part-ii
Manacher算法(http://blog.csdn.net/hk2291976/article/details/51107886)
Answer:
- O(n^3) 两侧比较法
public class Solution { public String longestPalindrome(String s) { int maxPalinLength = 0; String longestPalindrome = s; int length = s.length(); // check all possible sub strings for (int i = 0; i < length; i++) { for (int j = i + 1; j < length; j++) { int len = j - i; String curr = s.substring(i, j + 1); if (isPalindrome(curr)) { if (len > maxPalinLength) { longestPalindrome = curr; maxPalinLength = len; } } } } return longestPalindrome; } public static boolean isPalindrome(String s) { for (int i = 0; i < s.length() - 1; i++) { if (s.charAt(i) != s.charAt(s.length() - 1 - i)) { return false; } } return true; } }
- 中心扩展法
public class Solution { public String longestPalindrome(String s) { if (s == null || s.length() < 2) { return s; } String longest = s.substring(0, 1); for (int i = 0; i < s.length(); i++) { // get longest palindrome with center of i String tmp = helper(s, i, i); if (tmp.length() > longest.length()) { longest = tmp; } // get longest palindrome with center of i, i+1 tmp = helper(s, i, i + 1); if (tmp.length() > longest.length()) { longest = tmp; } } return longest; } // Given a center, either one letter or two letter, Find longest palindrome public static String helper(String s, int begin, int end) { while (begin >= 0 && end <= s.length() - 1 && s.charAt(begin) == s.charAt(end)) { begin--; end++; } return s.substring(begin + 1, end); } }
- 动态规划
public class Solution { public String longestPalindrome(String s) { if (s == null || s.length() < 2) { return s; } int maxLength = 0; String longest = null; int length = s.length(); boolean[][] table = new boolean[length][length]; // 单个字符都是回文 for (int i = 0; i < length; i++) { table[i][i] = true; longest = s.substring(i, i + 1); maxLength = 1; } // 判断两个字符是否是回文 for (int i = 0; i < length - 1; i++) { if (s.charAt(i) == s.charAt(i + 1)) { table[i][i + 1] = true; longest = s.substring(i, i + 2); maxLength = 2; } } // 求长度大于2的子串是否是回文串 for (int len = 3; len <= length; len++) { for (int i = 0, j; (j = i + len - 1) <= length - 1; i++) { if (s.charAt(i) == s.charAt(j)) { table[i][j] = table[i + 1][j - 1]; if (table[i][j] && maxLength < len) { longest = s.substring(i, j + 1); maxLength = len; } } else { table[i][j] = false; } } } return longest; }}
- 马拉车
// Transform S into T.// For example, S = "abba", T = "^#a#b#b#a#$".// ^ and $ signs are sentinels appended to each end to avoid bounds checkingstring preProcess(string s) { int n = s.length(); if (n == 0) return "^$"; string ret = "^"; for (int i = 0; i < n; i++) ret += "#" + s.substr(i, 1); ret += "#$"; return ret;}string longestPalindrome(string s) { string T = preProcess(s); int n = T.length(); int *P = new int[n]; int C = 0, R = 0; for (int i = 1; i < n-1; i++) { int i_mirror = 2*C-i; // equals to i' = C - (i-C) P[i] = (R > i) ? min(R-i, P[i_mirror]) : 0; // Attempt to expand palindrome centered at i while (T[i + 1 + P[i]] == T[i - 1 - P[i]]) P[i]++; // If palindrome centered at i expand past R, // adjust center based on expanded palindrome. if (i + P[i] > R) { C = i; R = i + P[i]; } } // Find the maximum element in P. int maxLen = 0; int centerIndex = 0; for (int i = 1; i < n-1; i++) { if (P[i] > maxLen) { maxLen = P[i]; centerIndex = i; } } delete[] P; return s.substr((centerIndex - 1 - maxLen)/2, maxLen);}
- LeetCode 5. Longest Palindromic Substring
- LeetCode --- 5. Longest Palindromic Substring
- LeetCode 5.Longest Palindromic Substring
- [Leetcode] 5. Longest Palindromic Substring
- [LeetCode]5.Longest Palindromic Substring
- [leetcode] 5. Longest Palindromic Substring
- LeetCode-5. Longest Palindromic Substring
- leetcode 5. Longest Palindromic Substring
- leetcode 5. Longest Palindromic Substring
- Leetcode 5. Longest Palindromic Substring
- LeetCode 5. Longest Palindromic Substring
- Leetcode 5. Longest Palindromic Substring
- leetcode 5. Longest Palindromic Substring
- LeetCode-5. Longest Palindromic Substring
- Leetcode 5. Longest Palindromic Substring
- leetcode 5. Longest Palindromic Substring
- LeetCode *** 5. Longest Palindromic Substring
- 【leetcode】5. Longest Palindromic Substring
- java多线程入门
- 第十周-阅读程序
- 自定义View之绘图篇(三):文字(Text)
- AIDL
- C++第五次实验
- LeetCode--5. Longest Palindromic Substring
- 深入学习Java虚拟机之——垃圾收集算法与垃圾收集器
- 屏幕适配方案--代码自动生成文件
- C++第四次实验——派生和继承1
- CocoaPods的安装使用和常见问题
- LeetCode 019 Remove Nth Node From End of List
- An internal error occurred 和 java.lang.NullPointerException
- 面试中的排序算法总结
- shell awk高级应用