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 }
  • 相关阅读:
    Http相应代码及获取方法
    (转) SQL 命令
    vue 设置动态class
    elment-ui中 给table表格 某项加点击事件
    VUE 中封装组件
    导出 搜索出的对应条件下的 内容(地址栏中携带多多个数据)
    element ui+vue 后台管理系统的新增数据页面崩溃(处理下拉数据过多)
    VUE+百度地图 vue-baidu-map
    用javascript制作秒表(有一个小问题,复位的时候,毫秒从01开始)
    左右两列导航分开滑动(移动端中)
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/9207588.html
Copyright © 2011-2022 走看看