题目

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. 如何对vue项目进行优化,加快首页加载速度

    上个月上线了一个vue小项目,刚做完项目,打包上线之后,传到服务器上发现首页加载巨慢. 由于开发时间比较紧,我想着怎么快怎么来,因而在开发过程中没考虑过优化性能问题,酿成最后在带宽5M的情况下页面加载 ...

  2. JDBC之 自增长与事务

    JDBC之 自增长与事务 1.自增长 有这样一个现象:数据库中有两个表格 学生表(学生姓名,所在班级),班级表(班级号(自增长的主键),班级人数). 现在我往班级表插入一条信息, 只提供班级人数,班级 ...

  3. 常见的网络攻击(XSS,SQL注入,CSRF)

    一.XSS 二.SQL注入 三.CSRF

  4. flask run方法和run_simple

    1.Flask提供的Web服务器不适合在生产环境中使用 2.run方法启动flask集成的服务器: 例: if __name__ == '__main__': app.run(debug=True) ...

  5. 20172308《Java软件结构与数据结构》第四周学习总结

    教材学习内容总结 第 6 章 列表 一. 列表集合 列表集合:一种概念性表示法,思想是使事物以线性列表的方式进行组织 特点: 列表集合没有内在的容量大小,它可以随着需要而增大 列表集合更具一般化,可以 ...

  6. 【NOI2005】聪聪和可可 概率与期望 记忆化搜索

    1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1635  Solved: 958[Submit][Statu ...

  7. OpenJ_POJ C16G Challenge Your Template 迪杰斯特拉

    Challenge Your Template 题目连接: http://acm.hust.edu.cn/vjudge/contest/122701#problem/G Description ACM ...

  8. 关于tensorflow中tensorborad No dashboards are active for the current data set.的解决办法

    说明:这个问题,困惑了好久,在网上查了很久,一直没能解决,直到我在stackoverflow上看到有一位博主的回答 链接在这里:(https://stackoverflow.com/questions ...

  9. 2015 年度新增开源软件排名 TOP 100 - 开源中国社区

    2015 年度新增开源软件排名 TOP 100 - 开源中国社区 39.ABTestingGateway http://www.oschina.net/news/69808/2015-annual-r ...

  10. 前端构建和模块化工具-coolie

    [前言] 假设你之前用过前端模块化工具:seajs.requirejs. 用过前端构建工具grunt.gulp, 而且感到了一些不方便和痛苦,那么你能够试试coolie [coolie] 本文不是一篇 ...