题目

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. NOI.AC NOIP模拟赛 第四场 补记

    NOI.AC NOIP模拟赛 第四场 补记 子图 题目大意: 一张\(n(n\le5\times10^5)\)个点,\(m(m\le5\times10^5)\)条边的无向图.删去第\(i\)条边需要\ ...

  2. KVM源代码解读:linux-3.17.4\arch\x86\include\asm\kvm_host.h

    /* * Kernel-based Virtual Machine driver for Linux * * This header defines architecture specific int ...

  3. Beyond Compare 4提示已经过了30天试用期,破解方式,亲测可用

    修改注册表 1)在搜索栏中输入 regedit ,打开注册表 2) 删除项目:计算机\HKEY_CURRENT_USER\Software\Scooter Software\Beyond Compar ...

  4. iOS图片设置圆角性能优化

    问题 圆角虽好,但如果使用不当,它就是你的帧数杀手,特别当它出现在滚动列表的时候.下面来看圆角如何毁掉你的流畅度的. 实测 layer.cornerRadius 我创建了一个简单地UITableVie ...

  5. Docker系列之(四):Win10上运行Docker

    1. 前言 Docker最近推出了可以运行在Win10和Mac上的稳定版本,让我们赶紧来体验一下. 2. 安装准备 需要的条件为: 64bit Windows 10,开启Hyper-V 2.1 下载D ...

  6. Tomcat无法启动8005端口,提示:java.net.ConnectException: 拒绝连接 (Connection refused)

    修改$JAVA_HOME/jre/lib/security/Java.security 文件中 securerandom.source 配置项: 将 securerandom.source=file: ...

  7. CentOS以守护进程的方式启动程序的另类用法daemon

    在Linux下如果以守护进程的方式启动程序,也就是后台运行,有几种方式,比如后面带&&,nuhop,那么在CentOS下可以使用daemon这个函数来启动,比如: daemon --p ...

  8. ORM for Net主流框架汇总与效率测试

    框架已经被越来越多的人所关注与使用了,今天我们就来研究一下net方面的几个主流ORM框架,以及它们的效率测试(可能会有遗漏欢迎大家讨论). ORM框架:Object/Relation Mapping( ...

  9. Message Queue协议AMQP

    历史: Message Queue的需求由来已久,80年代最早在金融交易中,高盛等公司采用Teknekron公司的产品,当时的Message queuing软件叫做:the information b ...

  10. LPC43xx SGPIO Slice 输入输出连接表