题目

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++) {

             int len = j - i;

             String curr = s.substring(i, j + 1);

             if (isPalindrome(curr)) {

                 if (len > maxPalinLength) {

                     longestPalindrome = curr;

                     maxPalinLength = len;

                 }

             }

         }

     }

  

     return longestPalindrome;

 }

  

 public boolean isPalindrome(String s) {

  

     for (int i = 0; i < s.length() - 1; i++) {

         if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {

             return false;

         }

     }

  

     return true;

 }

第二种方法“是对于每个子串的中心(可以是一个字符,或者是两个字符的间隙,比如串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             
             if (tmp.length() > longest.length())
                 longest = tmp;
      
             // get longest palindrome with center of i, i+1
             tmp = helper(s, i, i + 1);
             if (tmp.length() > longest.length())
                 longest = tmp;
         }
      
         return longest;
     }
      
     // Given a center, either one letter or two letter, 
     // Find longest palindrome
     public String helper(String s, int begin, int end) {
         while (begin >= 0 && end <= s.length() - 1 && s.charAt(begin) == s.charAt(end)) {
             begin--;
             end++;
         }
         return s.substring(begin + 1, end);
     }

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

Longest Palindromic Substring leetcode java的更多相关文章

  1. leetcode 第五题 Longest Palindromic Substring (java)

    Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...

  2. Longest Palindromic Substring——LeetCode

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  3. Longest Palindromic Substring -LeetCode

    题目 Given a string s,find the longest palindromic substring in S.You may assume  that the maximum len ...

  4. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  5. Leetcode: Longest Palindromic Substring. java

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  6. Java [leetcode 5] Longest Palindromic Substring

    问题描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  7. LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划

    题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...

  8. leetcode:Longest Palindromic Substring(求最大的回文字符串)

    Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

  9. LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion

    1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...

随机推荐

  1. 错误跳转js

    <script type="text/javascript"> var t = 5; //倒计时的秒数 function showTime(){ document.ge ...

  2. poj 3660 传递闭包 **

    题意:题目给出了m对的相对关系,求有多少个排名是确定的. 链接:点我 如果这个点到其他点的关系是确定的,那么这个点就是确定的,注意如果这个点到不了其他点,但其他点能到这个点,那么这个点和其他点的关系是 ...

  3. 【转载】gdi+ 内存泄漏

    [转载]http://issf.blog.163.com/blog/static/1941290822009111894413472/ 最近用GDI+实现了几个自定义控件,但是发现存在内存泄露问题 B ...

  4. 9、Redis处理过期keys的机制

    写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------- 1.Redis处理过期k ...

  5. Linux命令中,$、#、@、0、1、2、*、?的作用

    $# 是传给脚本的参数个数 $0 是脚本本身的名字   $1 是传递给该shell脚本的第一个参数   $2 是传递给该shell脚本的第二个参数   $@ 是传给脚本的所有参数的列表   $* 是以 ...

  6. 【原】Spring整合Redis(第一篇)—SDR简述

    1.SDR说明 Spring Data Redis(SDR),是SpringFramework提供的一套简化访问Redis的API,是对Jedis的又一层封装. SDR集成了Jedis,JRedis, ...

  7. ant design table column 设置width不生效解决方案

    当td里的内容超出了width的范围时,会出现width不固定,也就是width不生效 解决方案: 设置scroll的width等于所有列宽之和(scroll={{x: 100%}})

  8. @Transactional导致AbstractRoutingDataSource动态数据源无法切换的解决办法

    上午花了大半天排查一个多数据源主从切换的问题,记录一下: 背景: 项目的数据库采用了读写分离多数据源,采用AOP进行拦截,利用ThreadLocal及AbstractRoutingDataSource ...

  9. 玩转ptrace(转)

    下面是转帖的内容,写的很详细.但是不同的linux发行版中头文件的路径和名称并不相同.如在某些发行版中<linux/user.h>就不存在,其中定义的变量出现在<asm/ptrace ...

  10. LPC43xx SGPIO Camera interface design

    AN11196: Camera interface design using SGPIO