• LeetCode 5. 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 SubstringsLongest Palindromic Subsequence.

  • 相关阅读:
    Android如何防止apk程序被反编译
    Android的十六进制颜色值
    popupwindow使用之异常:unable to add window -- token null is not valid
    布局文件中fill_parent和match_parent有什么区别?
    Android:Layout_weight的深刻理解
    页面的五种布局以及嵌套『Android系列八』
    禁止危险函数
    表单令牌防止重复提交原理
    【ThinkPHP框架3.2版本学习总结】四、视图
    【ThinkPHP框架3.2版本学习总结】三、模型
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4921988.html
Copyright © 2020-2023  润新知