【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 ...
随机推荐
- 小学生都能学会的python(字典{ })
小学生都能学会的python(字典{ }) 1. 什么是字典 dict. 以{}表示. 每一项用逗号隔开, 内部元素用key:value的形式来保存数据 {"jj":"林 ...
- java有参无参构造器的的执行顺序
这里拿了用数组构造栈的一段代码说明一下 public class StackArray<E> { private Object[] data = null; private int max ...
- Xdoclet + Ant自己主动生成Hibernate配置文件
在使用Hibernate的时候,过多的Hibernate配置文件是一个让人头疼的问题. 近期接触了Xdoclet这个工具. 它实际上就是一个自己主动代码生成的工具.Xdoclet不能单独执行,必须搭配 ...
- 基于sparksql调用shell脚本运行SQL
[Author]: kwu 基于sparksql调用shell脚本运行SQL,sparksql提供了类似hive中的 -e , -f ,-i的选项 1.定时调用脚本 #!/bin/sh # uplo ...
- 一个人的旅行 HDU杭电2066【dijkstra算法 || SPFA】
pid=2066">http://acm.hdu.edu.cn/showproblem.php? pid=2066 Problem Description 尽管草儿是个路痴(就是在杭电 ...
- vue 结合 echarts
http://blog.csdn.net/mr_wuch/article/details/70225364
- Redis 安装与简单示例 <第一篇>【转】
一.Redis的安装 Redis下载地址如下:https://github.com/dmajkic/redis/downloads 解压后根据自己机器的实际情况选择32位或者64位.下载解压后图片如下 ...
- js小知识 双叹号(!!)
!!:一般用来将后面的表达式强制转换为布尔值(boolean):true或者false; avascript约定规则为: false.undefinded.null.0.”” 为 false tr ...
- Codeforces 988E. Divisibility by 25
解题思路: 只有尾数为25,50,75,00的数才可能是25的倍数. 对字符串做4次处理,以25为例. a. 将字符串中的最后一个5移到最后一位.计算交换次数.(如果没有找到5,则不可能凑出25,考虑 ...
- JOSN快速入门
1.JSON介绍 (1)JSON是一种与开发语言无关的,轻量级的数据格式,全称 JavaScript Object Notation,易于阅读和编写,语言解析和生产 (2)JSON数据类型表示 数据 ...