LeetCode Longest Palindromic Substring
原題鏈接在這里:https://leetcode.com/problems/longest-palindromic-substring/
題目:
Given a string?s, find the longest palindromic substring in?s. You may assume that the maximum length of?s?is 1000.
Example:
Input: "babad"Output: "bab"Note: "aba" is also a valid answer.?Example:
Input: "cbbd"Output: "bb"題解:
以字符串的每一個char 為中心開始向左右延展,直到不是回文為止,若新的substring 比原來的res長,就更新res.
Note: 子字符串的中心可以是一個字符,整個字符串有奇數個字符;也可以是兩個字符中間的空隙,整個字符串有偶數個字符。
Note: longest輔助函數在左右延展時,跳出while loop時,i 和 j 指向的要么是out of index bound, 要么指向的字符已經不想等了。所以最后返回的最常字符串應該是substring(i+1,j)這一段,?j 指向的 字符并沒有包括在最后結果中。
Time Complexity: O(n^2). n = s.length(). Space: O(n), res的長度。
AC Java:?
1 public class Solution { 2 public String longestPalindrome(String s) { 3 if(s == null || s.length() == 0){ 4 return s; 5 } 6 String res = ""; 7 for(int i = 0; i<s.length(); i++){ 8 //string with odd # of characters 9 String longestOddSubstring = findLongest(s, i, i); 10 if(longestOddSubstring.length() > res.length()){ 11 res = longestOddSubstring; 12 } 13 //string with even # of characters 14 String longestEvenSubstring = findLongest(s, i, i+1); 15 if(longestEvenSubstring.length() > res.length()){ 16 res = longestEvenSubstring; 17 } 18 } 19 return res; 20 } 21 22 //find the longest palindromic substring 23 private String findLongest(String s, int i, int j){ 24 while(i>=0 && j<s.length() && s.charAt(i) == s.charAt(j)){ 25 i--; 26 j++; 27 } 28 return s.substring(i+1,j); 29 } 30 }可以用index取得結果來節省空間. 當前index 為i向兩邊延展后取得的最長palindrome substring的長度len.
最長palindrome substring的左側index就是i-(len-1)/2, 右側index就是i+len/2.?
Time Complexity: O(n^2). n = s.length().
Space: O(1).
AC Java:
1 class Solution { 2 public String longestPalindrome(String s) { 3 if(s == null || s.length() == 0){ 4 return s; 5 } 6 7 int l = 0; 8 int r = 0; 9 for(int i = 0; i<s.length(); i++){ 10 int longestOddSubstringLen = findLongest(s, i, i); 11 int longestEvenSubstringLen = findLongest(s, i, i+1); 12 int longestSubstringLen = Math.max(longestOddSubstringLen, longestEvenSubstringLen); 13 if(longestSubstringLen > r-l+1){ 14 l = i-(longestSubstringLen-1)/2; 15 r = i+longestSubstringLen/2; 16 } 17 } 18 return s.substring(l, r+1); 19 } 20 21 private int findLongest(String s, int i, int j){ 22 while(i>=0 && j<s.length() && s.charAt(i)==s.charAt(j)){ 23 i--; 24 j++; 25 } 26 return j-i-1; 27 } 28 }DP 方法. dp[i][j]記錄s.substring(i, j+1)是否為Palindromic.
dp[i][j] = (s.charAt(i) == s.charAt(j) && (j-i<2 || dp[i+1][j-1]).
Time Complexity: O(n^2). Space: O(n^2).
1 public class Solution { 2 public String longestPalindrome(String s) { 3 if(s == null || s.length() == 0){ 4 return s; 5 } 6 7 String res = ""; 8 int len = s.length(); 9 boolean [][] dp = new boolean[len][len]; 10 for(int i = len-1; i>=0; i--){ 11 for(int j = i; j<len; j++){ 12 //Need to check j-i < 2 first 13 //or IndexOutOfBondException 14 if(s.charAt(i) == s.charAt(j) && (j-i<2 || dp[i+1][j-1])){ 15 dp[i][j] = true; 16 if(j-i+1 > res.length()){ 17 res = s.substring(i, j+1); 18 } 19 } 20 } 21 } 22 return res; 23 } 24 }類似Palindromic Substrings,?Longest Palindromic Subsequence.
轉載于:https://www.cnblogs.com/Dylan-Java-NYC/p/4921988.html
總結
以上是生活随笔為你收集整理的LeetCode Longest Palindromic Substring的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android双击返回键退出程序的实现
- 下一篇: (三)页面之间的参数传递