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.

给一个String串,求其中最大的回文字串.这是一个典型的最长回文子串问题,目前有四种解法.

1.暴力测试,测试每个子串,显然这样是最笨的方法,时间复杂度O(n^3)

2.动态规划,可以参考http://blog.163.com/zhaohai_1988/blog/static/2095100852012716105847112/http://www.cnblogs.com/en-heng/p/3973679.html

3.中心扩展法,这也是我看到题目之后最先想到的,以一个元素为中心,分别向前和向后扩展,看是否相同,要考虑abcba形式  abccba形式这两种不同的回文子串形式,时间复杂度显然是O(n^2).

 1 public static String matchChar(String s,int l,int r){   //匹配以l和r为下标的字符是否相同
 2         int left=l;
 3         int right=r;
 4         while(left<=right&&left>=0&&right<s.length()&&s.charAt(left)==s.charAt(right)){
 5             left--;
 6             right++;
 7         }
 8         return  s.substring(left+1,right);
 9     }
10     public  String longestPalindrome(String s) {
11         int n=s.length();
12         if(0==n) return "";
13         String longestString=s.substring(0,1);//一个字符
14         for(int i=0;i<n-1;i++){
15             String tempString1=matchChar(s, i, i);  //abcba形式
16             if(tempString1.length()>longestString.length())
17                 longestString=tempString1;
18             String tempString2=matchChar(s,i,i+1);  //abccba形式
19             if(tempString2.length()>longestString.length())
20                 longestString=tempString2;
21         }
22         return longestString;
23     }
4.传说中的Manacher算法,时间复杂度O(n),也是比较难以理解的算法.看了好几篇博客,想要了解可以参考http://blog.csdn.net/ggggiqnypgjg/article/details/6645824http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html
				
时间: 2024-08-05 08:52:29

LeetCode5 Longest Palindromic Substring的相关文章

[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-5. Longest Palindromic Substring(M)

解法 比较经典的问题,寻找最长回文子串.Leetcode里提供了多种解法.我采用最直观的解法:中心扩展法. 思路是每次以当前元素为中心向两边扩展,直到遇到不同元素,此时找到一个子串.有两点需要注意的地方: 1)空串和单字符都是回文,直接返回即可. 2)偶数回文和奇数回文的情况.例如:abace是aba的奇数串,以b为中心向两周扩展即可.但是abbawe是偶数串abba,这样需要以bb为指针向两周同时往外扩展. Given a string s, find the longest palindro

LeetCode-5:Longest Palindromic Substring(最长回文子字符串)

描述:给一个字符串s,查找它的最长的回文子串.s的长度不超过1000. Input: "babad" Output: "bab" Note: "aba" is also a valid answer. 我是采用动态规划解决此题的.官方的solutions中提供了几种思路,包括我使用的DP.这里摘要如下: 思路1: 将s反转得到s',然后查找s和s'的最长公共子串substring,那么substring就是最长回文子串.比如:s = "

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. 解题思路: 主要有三种: 第一种:Manacher算法,也是最快的,时间复杂度为O(n) 第二种:DP算法,时间复杂度为O(n*n) 第三种:

[string]Longest Palindromic Substring

Total Accepted: 82026 Total Submissions: 379898 Difficulty: Medium 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. Subscrib

LeetCode OJ: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. 玩了两天dota2,罪过罪过,还是应该老老实实刷题啊. 题目求得是最长的回文子串,这里使用的是暴力的解法,也就是解决两种回文"asdsa"以

5. Longest Palindromic Substring - Unsolved

https://leetcode.com/problems/longest-palindromic-substring/#/description 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: &

[Leetcode] 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. 做这道题之前要先了解什么是回文子串.回文串通俗的解释是,分别从字符串两端开始遍历,得到的结果相同,如"abba",从两端的遍历结果都是:&q

【leedcode】 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. https://leetcode.com/problems/longest-palindromic-substring/ 求最大回文的长度,其实这道题