zoukankan      html  css  js  c++  java
  • Longest Palindromic Substring leetcode java

    题目

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

    题解

     第一种方法就是挨个检查,维护全局最长,时间复杂度为O(n3),会TLE。

     代码如下:

     1 public String longestPalindrome(String s) {
     2  
     3     int maxPalinLength = 0;
     4     String longestPalindrome = null;
     5     int length = s.length();
     6  
     7     // check all possible sub strings
     8     for (int i = 0; i < length; i++) {
     9         for (int j = i + 1; j < length; j++) {
    10             int len = j - i;
    11             String curr = s.substring(i, j + 1);
    12             if (isPalindrome(curr)) {
    13                 if (len > maxPalinLength) {
    14                     longestPalindrome = curr;
    15                     maxPalinLength = len;
    16                 }
    17             }
    18         }
    19     }
    20  
    21     return longestPalindrome;
    22 }
    23  
    24 public boolean isPalindrome(String s) {
    25  
    26     for (int i = 0; i < s.length() - 1; i++) {
    27         if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
    28             return false;
    29         }
    30     }
    31  
    32     return true;
    33 }

    第二种方法“是对于每个子串的中心(可以是一个字符,或者是两个字符的间隙,比如串abc,中心可以是a,b,c,或者是ab的间隙,bc的间隙,例如aba是回文,abba也是回文,这两种情况要分情况考虑)往两边同时进 行扫描,直到不是回文串为止。假设字符串的长度为n,那么中心的个数为2*n-1(字符作为中心有n个,间隙有n-1个)。对于每个中心往两边扫描的复杂 度为O(n),所以时间复杂度为O((2*n-1)*n)=O(n^2),空间复杂度为O(1)。”引自Code ganker(http://codeganker.blogspot.com/2014/02/longest-palindromic-substring-leetcode.html)

    代码如下:

     1     public String longestPalindrome(String s) {
     2         if (s.isEmpty()||s==null||s.length() == 1)
     3             return s;
     4      
     5         String longest = s.substring(0, 1);
     6         for (int i = 0; i < s.length(); i++) {
     7             // get longest palindrome with center of i
     8             String tmp = helper(s, i, i);
     9             
    10             if (tmp.length() > longest.length())
    11                 longest = tmp;
    12      
    13             // get longest palindrome with center of i, i+1
    14             tmp = helper(s, i, i + 1);
    15             if (tmp.length() > longest.length())
    16                 longest = tmp;
    17         }
    18      
    19         return longest;
    20     }
    21      
    22     // Given a center, either one letter or two letter, 
    23     // Find longest palindrome
    24     public String helper(String s, int begin, int end) {
    25         while (begin >= 0 && end <= s.length() - 1 && s.charAt(begin) == s.charAt(end)) {
    26             begin--;
    27             end++;
    28         }
    29         return s.substring(begin + 1, end);
    30     }

     Reference:

     http://www.programcreek.com/2013/12/leetcode-solution-of-longest-palindromic-substring-java/

     http://codeganker.blogspot.com/2014/02/longest-palindromic-substring-leetcode.html

  • 相关阅读:
    Maven的pom文件依赖提示 ojdbc6 Missing artifact,需要手动下载并导入maven参考
    Maven全局配置文件settings.xml详解(转)
    SpringBoot -- 项目结构+启动流程
    64匹马,8个赛道,找出前4名最少比赛多少场?——最快10次,最慢11次;
    Spring家族主流成员介绍
    java 中文与unicode互转
    Netty的Marshalling编解码器
    解决svn迁移过程中出现:SVN Error: is not the same repository as的问题
    netty入门实例
    Dubbo的使用及原理
  • 原文地址:https://www.cnblogs.com/springfor/p/3889209.html
Copyright © 2011-2022 走看看