• [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 }
  • 相关阅读:
    概率统计(DP)
    iOS中几种定时器
    微信开发笔记——微信网页登录授权,获取用户信息
    swift中通知的使用
    Swift的基础,操作符,字符串和集合类型
    NSNotificationCenter
    IOS中通知中心(NSNotificationCenter)的使用总结
    Swift观察者模式
    swift中通知的使用
    Swift
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/9207588.html
Copyright © 2020-2023  润新知