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 }
  • 相关阅读:
    【 D3.js 选择集与数据详解 — 5 】 处理模板的应用
    阿里云至 Windows Azure 的 Linux 虚拟机迁移
    【 随笔 】 JavaScript 图形库的流行度调查
    2015年,新的启程
    【 D3.js 选择集与数据详解 — 4 】 enter和exit的处理方法以及处理模板
    【 随笔 】 财源滚滚
    HelloXV1.77网络功能简介
    【 D3.js 选择集与数据详解 — 3 】 绑定数据的顺序
    【 D3.js 选择集与数据详解 — 2 】 使用data()绑定数据
    保持与 Microsoft Azure Files 的连接
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/9207588.html
Copyright © 2011-2022 走看看