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++) {
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的更多相关文章
- leetcode 第五题 Longest Palindromic Substring (java)
Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...
- Longest Palindromic Substring——LeetCode
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- Longest Palindromic Substring -LeetCode
题目 Given a string s,find the longest palindromic substring in S.You may assume that the maximum len ...
- 【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 ...
- Leetcode: Longest Palindromic Substring. java
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- Java [leetcode 5] Longest Palindromic Substring
问题描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划
题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- 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 ...
随机推荐
- BeagleBone Black教程之BeagleBone Black使用到的Linux基础
BeagleBone Black教程之BeagleBone Black使用到的Linux基础 BeagleBone Black涉及到的Linux基础 在许多没有Linux相关经验的人看来,Linux看 ...
- bzoj1205: [HNOI2005]星际贸易
题目链接 bzoj1205: [HNOI2005]星际贸易 题解 辣鸡题面,毁我青春 辣鸡题面,毁我青 辣鸡题面,毁我 辣鸡题面,毁 第一问,背包dp 第二问 问题转化为在一个序列上经过好多点走到终点 ...
- 李善友《认知升级之第一性原理》--507张PPT全解!_搜狐科技_搜狐网
http://www.sohu.com/a/151470602_733114
- 39、ABTestingGateway
2015 年度新增开源软件排名 TOP 100 - 开源中国社区 http://www.oschina.net/news/69808/2015-annual-ranking-top-100-new ...
- BZOJ 1207 DP
打一次鼹鼠必然是从曾经的某一次打鼹鼠转移过来的 以打每一个鼹鼠时的最优解为DP方程 #include<iostream> #include<cstdio> #include&l ...
- 解决svn中文乱码的问题
需要的工具:sqlitexiaz 工具下载: 链接:https://pan.baidu.com/s/1cz1Pvw 密码:yp64 1 首先在项目的根目录下,找到.svn(如果找不到,需要设置将隐藏文 ...
- 【Go命令教程】命令汇总
[Go命令教程]1. 标准命令详解 [Go命令教程]2. go build [Go命令教程]3. go install [Go命令教程]4. go get [Go命令教程]5. go clean [G ...
- CRC校验的实现
本例提供的是通过查表发来实现CRC校验. CRC余式表如下: unsigned int crctab[256] ={/*CRC余式表 */ 0x0000, 0x1021, 0x2042, 0x3063 ...
- delphi 文件查找
FindFirst 是用来寻找目标目录下的第一个文件, FindFirst函数在delphi帮助下的定义: function FindFirst(const Path: string; Attr: ...
- Unity3D实践系列06,球体撞击物体游戏
本篇实现一个球体在固定区域移动撞击Cube的游戏. 首先有1个Plane当作地面,1个Sphere当作球体,4个Cube当作墙,12个Cube当作被撞击物体,另外还有球体的撞击计算,在撞击的过程适时显 ...