zoukankan      html  css  js  c++  java
  • [leetcode]5. 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 1:

    Input: "babad"
    Output: "bab"
    Note: "aba" is also a valid answer.

    Example 2:

    Input: "cbbd"
    Output: "bb"

    题意:

    最长回文子串

    Solution1:  Brute Force. We can run three loops, the outer two loops pick all substrings one by one by locking the corner characters, the inner loop checks whether the picked substring is palindrome or not. 

    code:

     1 /*
     2     Time complexity: O ( n^3 )  outer->2 for loops to find all possible substrings; 
     3                                 inner->1 while loop to check current substring isValidPalindrome
     4     Space complexity: O ( 1 )
     5 */
     6 
     7 class Solution {
     8     public String longestPalindrome(String s) {
     9         String res = "";
    10         for (int i = 0; i < s.length(); i++) {
    11             for (int j = i ; j < s.length(); j++) {  
    12                 String sub = s.substring(i, j + 1);
    13                 if (isValidPalindrome(sub) && sub.length() > res.length()) {
    14                     res = sub;
    15                 }
    16             }
    17         }
    18         return res;
    19     }
    20 
    21     public boolean isValidPalindrome(String s){
    22         int l = 0;
    23         int r = s.length() - 1;
    24         while (l < r){
    25             if(s.charAt(l) != s.charAt(r)){
    26                 return false;
    27             }else{
    28                 l++;
    29                 r--;
    30             }
    31         }
    32         return true;
    33     }
    34 }

    Solution2:  DP.

    step1, initialize a matrix for saving intermediate info

    step2, we can pre-calculate some info(此题代码可以将预处理合并到general case中来写)

    step3, To fill the matrix.  If  char at start != char at end,  then s.substring[start, end] cannot be a palindrom, fill 'F' in such spot

    step4, To fill the matrix.  If  char at start == char at end, it means two sides are the same. Then if we can make sure substring [start + 1 to end - 1] is panlindrom,  the whole substring should be a panlindrom.

    step5, to fill the matrix in the same way

    step6, update the longest result

     code

     1 class Solution {
     2     public String longestPalindrome(String s) {
     3         String res = "";
     4         boolean[][] dp = new boolean[s.length()][s.length()];
     5         int max = 0;
     6         for(int j= 0; j < s.length(); j++){
     7             for(int i = 0; i<=j; i++){
     8                 dp[i][j] = s.charAt(i) == s.charAt(j) && ((j-i<=2)||dp[i+1][j-1]);      
     9                 if(dp[i][j] && (j-i+1>max)){
    10                 max = j- i + 1;
    11                 res = s.substring(i,j+1);
    12             }
    13            }  
    14         }     
    15        return res;     
    16     }
    17 }
  • 相关阅读:
    标题党的进步:道字大旗不再扯,美为号召又开张
    dwr自动生成的js文件到底在哪里?
    JavaScript全局优化带来的负面效果……
    内训资料公开:设计师的实战过程(1)
    元语言基础技术之:在JS中如何自由地创建函数
    QoBean的元语言系统(一)
    Oracle面向服务的架构
    对JavaScript的eval()中使用函数的进一步讨论~
    KEGG and Gene Ontology Mapping in Bioinformatic Method
    mysql user administration
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/9207588.html
Copyright © 2011-2022 走看看