【LeetCode】Longest Palindromic Substring 解题报告
DP、KMP什么的都太高大上了。自己想了个朴素的遍历方法。
【题目】
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(n)吧)
从中间向两端搜索。分别找到以每一个字母为中心的最长回文子串,假设两边剩余的元素已经比当前最长回文子串的一半还短时,停止遍历。
大家别看代码长。是为了便于理解的。
【Java代码】
public class Solution {
public String longestPalindrome(String s) {
int len = s.length();
if (s == null || len == 1){
return s;
}
String ret = "";
int mid = len / 2;
int i = 0;
while (true) {
//遍历到s两端时。假设以mid+i或mid-i为中心的最长回文都不比当前最优解长,遍历结束
if (2*(len-(mid+i)) < ret.length() && 2*(mid-i+1) < ret.length()) {
break;
}
String str1 = palindrome1(s, mid+i);
String str2 = palindrome2(s, mid+i);
String str3 = palindrome1(s, mid-i);
String str4 = palindrome2(s, mid-i);
if (str1.length() > ret.length()) {
ret = str1;
}
if (str2.length() > ret.length()) {
ret = str2;
}
if (str3.length() > ret.length()) {
ret = str3;
}
if (str4.length() > ret.length()) {
ret = str4;
}
i++;
}
return ret;
}
//找出s中以index为中心的aba型的回文子串
public String palindrome1(String s, int index) {
String ret = "";
int i = index, j = index;
while (i>=0 && j<s.length()) {
if (s.charAt(i) != s.charAt(j)) {
break;
}
ret = s.substring(i, j+1);
i--;
j++;
}
return ret;
}
//找出s中以index和index+1为中心的abba型回文子串
public String palindrome2(String s, int index) {
String ret = "";
int i = index, j = index+1;
while (i>=0 && j<s.length()) {
if (s.charAt(i) != s.charAt(j)) {
break;
}
ret = s.substring(i, j+1);
i--;
j++;
}
return ret;
}
}
【分析】
后来在网上找了类似的解法。
如 http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html ,为了不用区分aba型和abba型的回文子串,构造了一个新的字符串 t ,在两端和每两个字母之间插入一个特殊字符 ‘#’ 。这样当以‘#’为中心的回文子串就是我的代码中abba型子串。
我的代码与之相比还使用了一个小trick,即从中间向两端遍历。这样假设中间已经有比較长的回文子串了,那么两端比較偏的回文子序列就可省去推断。
一遍就AC了。后来比較了网上的解法,不禁为自己有点小激动呢~。
分析可能有误,或许仅仅是自我感觉良好,欢迎大家指正!
【LeetCode】Longest Palindromic Substring 解题报告的更多相关文章
- Leetcode:【DP】Longest Palindromic Substring 解题报告
Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...
- [LeetCode] 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
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [LeetCode] Longest Palindromic Substring(manacher algorithm)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- C++ leetcode Longest Palindromic Substring
明天就要上课了,再过几天又要见班主任汇报项目进程了,什么都没做的我竟然有一种迷之淡定,大概是想体验一波熬夜修仙的快乐了.不管怎么说,每天还是要水一篇博文,写一个LeetCode的题才圆满. 题目:Gi ...
- Leetcode: Longest Palindromic Substring && Summary: Palindrome
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- LeetCode:Longest Palindromic Substring 最长回文子串
题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- Leetcode: Longest Palindromic Substring. java
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- LeetCode——Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
随机推荐
- 110_leetcode_Best Time to Buy and sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- B树索引与索引优化
B树索引与索引优化 MySQL的MyISAM.InnoDB引擎默认均使用B+树索引(查询时都显示为“BTREE”),本文讨论两个问题: 为什么MySQL等主流数据库选择B+树的索引结构? 如何基于索引 ...
- C# 导出excel的压缩包到浏览器页面
需求背景:TCX_1710项目产品质量导出功能,客户希望每个总成导出到一个Excel表中 实现分析:客户选择时间段,点击导出按钮,默认导出开始时间当天的数据,每个总成一个Excel,将各个Excel打 ...
- jqGrid添加删除功能(不和数据库交互)
jqGrid添加删除功能(不和数据库交互) 一.背景需求 项目中需要在前端页面动态的添加行,删除行,上下移动行等,同时还不和数据库交互.一直在用jqGrid展示表格的我们,从没有深入的研究过它,当然看 ...
- iOS Device Types
ios 设备硬件名称对照表 https://support.hockeyapp.net/kb/client-integration-ios-mac-os-x-tvos/ios-device-types ...
- Windows 安装PostgreSQL
下载二进制包:https://www.enterprisedb.com/download-postgresql-binaries 直接解压到C盘 Microsoft Windows [版本 6.3.9 ...
- Linux rm删除大批量文件遇到 Argument list too long
在使用rm删除大批量文件时,有可能会遭遇“参数列太长”(Argument list too long)的问题.如下所示 [oracle@DB-Server bdump]$ rm -v epps_q ...
- 优动漫PAINT-简单的树、叶教学
如题,简单.好用:其实说的还是一个观察的事.看你是否足够细心,对于树叶的生长.枝桠和树干的关系是否了解咯. 对于这样的树枝丫和叶子完全可以使用优动漫PAINT完成,简单又快捷,软件下载:www.don ...
- [Python随笔]>>字符串大小写是如何转换的?
首先看下Python的源码 Emmmm,说明是底层的C实现的,所以只放了说明 再看看别人家孩子的博客:https://blog.csdn.net/world6/article/details/6994 ...
- grant 命令
创建拥有所有权限账户.可以远程连接.并且允许用户再将该权限授予其它用户: grant all privileges on *.* to root @"%" identified b ...