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

Longest Palindromic Substring leetcode java,布布扣,bubuko.com

时间: 2024-10-15 04:00:38

Longest Palindromic Substring leetcode java的相关文章

Longest Palindromic Substring——LeetCode

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. 题目大意:给一个字符串S,存在唯一的最长的回文子串,求这个回文子串. 解题思路:首先这个题有O(n)的解,是在http://articles.leet

Longest Palindromic Substring[leetcode]

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. class Solution { public: string longestPalindrome(string s) { int len = s.l

leetcode longest palindromic substring (medium) /java

最长回文字串 上题: 测试用例中,注意aaabaaaa. 但是我time limit exceeded.用了极暴力的方法解.(三层循环)找每一个字符的最长回文字串. 1 /** 2 * 最长回文子串 3 * 2017-5-7 4 **/ 5 6 import java.io.*; 7 import java.util.*; 8 import java.lang.*; 9 10 public class Solution 11 { 12 public static String longestPa

LeetCode【5】. Longest Palindromic Substring --java实现

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, and there exists one unique longest palindromic substring. 题目要求给定字符串的最大对称子字符串,如"aaabccbac

Java for LeetCode 005 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, and there exists one unique longest palindromic substring. 解题思路一: 暴力枚举 共N^2量级个子串(从下标零开始),每次检查需一个for循环,等于是3重for循环,时间复杂度O(n^3) 解题思路二: 动态

Leetcode: Longest Palindromic Substring. 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. 动态规划public class Solution { public String longestPalindrome(String s) { if

LeetCode 005 Longest Palindromic Substring - Java

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. Example: Input: "cbbd" Ou

LeetCode 5 Longest Palindromic Substring(C,C++,Python,Java)

Problem: 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. Solution: 以每一个字符为中间查找回文串,然后记录下最大的回文串,时间复杂度O(n^2) 题目大意: 给定一个字符串,求字符

[LeetCode][Java]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, and there exists one unique longest palindromic substring. 题意: 给定字符串S,找到该字符串中最长的回文子串.假设这个子串的最大长度为1000,并且这个最长回文子串是存在且唯一的. 算法分析: 回文字