5. Longest Palindromic Substring java solutions

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.

 1 public class Solution {
 2     public String longestPalindrome(String s) {
 3         if(s == null || s.length() <= 1) return s;
 4         String ans = s.substring(0,1);
 5         for(int i = 0; i < s.length(); i++){
 6             String tmp = findPalindrome(s,i,i);//回文为奇数的情况
 7             if(tmp.length() > ans.length()) ans = tmp;
 8
 9             tmp = findPalindrome(s,i,i+1);// 回文为偶数的情况
10             if(tmp.length() > ans.length()) ans = tmp;
11         }
12         return ans;
13     }
14
15     public String findPalindrome(String str, int s, int e){
16         while(s >=0 && e < str.length() && str.charAt(s) == str.charAt(e)){
17             s--; e++;
18         }
19         return str.substring(++s,e);
20     }
21 }

时间复杂度O(N*(N+N+1))

本题也可以使用DP做法:

参考DP:http://blog.csdn.net/soszou/article/details/37312317

时间: 2024-11-06 20:19:06

5. Longest Palindromic Substring java solutions的相关文章

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

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 [java]

public String longestPalindrome(String s) { String rs = ""; int res = 0; for(int i = 0; i< s.length(); i++){ int left = i, right = i; while(left >= 0 && s.charAt(i) == s.charAt(left)) left --; while(right < s.length() &&

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 longestP

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) 题目大意: 给定一个字符串,求字符

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) 解题思路二: 动态

[Java]LeetCode5 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. 题意:求字符串中最长的回文 感觉这题不难,可以这样想,设置两个指针,分别对应0,len-1. 比如从第一个字符开始,abababac,我们可以找a出现

[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,并且这个最长回文子串是存在且唯一的. 算法分析: 回文字