Longest Palindromic Substring——LeetCode
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,存在唯一的最长的回文子串,求这个回文子串。
解题思路:首先这个题有O(n)的解,是在http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html这里有详细的解释,本文的算法是O(n^2)的,中规中矩的做法。回文有两种,abba和aba,本文分别对子串以i为中心进行检查偶数和奇数哪个长,同时记录长度,下标,奇偶等信息。
Talk is cheap>>
public String longestPalindrome(String s) {
if (s == null || s.length() <= 1) {
return s;
}
int max_len = 0, pos = 0;
boolean odd = false;
for (int i = 0; i < s.length(); i++) {
int forward = i - 1;
int backward = i + 1;
int len = 0;
while (forward >= 0 && backward < s.length() && s.charAt(forward) == s.charAt(backward)) {
forward--;
backward++;
len++;
}
if (max_len < (len * 2 + 1)) {
max_len = len * 2 + 1;
odd = true;
pos = i;
}
len = 0;
forward = i;
backward = i + 1;
while (forward >= 0 && backward < s.length() && s.charAt(forward) == s.charAt(backward)) {
forward--;
backward++;
len++;
}
if (max_len < len * 2) {
max_len = len * 2;
odd = false;
pos = i;
}
}
if (odd) {
return s.substring(pos - (max_len - 1) / 2, pos + (max_len - 1) / 2 + 1);
}
return s.substring(pos - max_len / 2 + 1, pos + max_len / 2 + 1);
}
Longest Palindromic Substring——LeetCode的更多相关文章
- Longest Palindromic Substring -LeetCode
题目 Given a string s,find the longest palindromic substring in S.You may assume that the maximum len ...
- Longest Palindromic Substring leetcode java
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- [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 5. Longest Palindromic Substring
写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...
- LeetCode 5 Longest Palindromic Substring(最长子序列)
题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...
- 【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(manacher algorithm)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
随机推荐
- 多线程下的performSelector和NSThread的使用
多线程下的performSelector和NSThread的使用 NSThread的多线程使用: 我们可以使用这两种方法来使用线程中的问题 - (id)initWithTarget:(id)targe ...
- 【JavaScript】我的JavaScript技术总结第一篇——编程细节
遍历数组 for (var i=0, l=arr.length; i<l; i++) 这样写的一个好处就是让每次循环少一步获取数组对象长度的操作,数组长度越长,价值越明显. 判断变量的真假 if ...
- [转] 使用CodeViz生成C/C++函数调用关系图
运行环境:虚拟机下的Ubuntu 11.04 结合Graphviz工具,使用CodeViz可以生成直观和漂亮的C/C++程序函数之间的调用关系图. 1.安装graphviz 在安装CodeViz之前, ...
- RxJava 教程-1 简介 原理 线程控制 变换
简介 RxJava 是什么? RxJava 在 GitHub 主页上的自我介绍是 RxJava is a Java VM implementation of ReactiveX: a library ...
- bootstrap的流式布局
Bootstrap---Fluid layout 流布局 流布局是一种适应屏幕的做法.即不固定块的宽度,而是采用百分比作为单位来确定每一块的宽度.这种布局非常适合一次编写,然后自适应各种不同大小的屏幕 ...
- 【转】Java学习之Iterator(迭代器)的一般用法 (转)
[转]Java学习之Iterator(迭代器)的一般用法 (转) 迭代器(Iterator) 迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭 ...
- SQL利用临时表实现动态列、动态添加列
--方法一--------------------------------------------------------------------- declare @sql as varchar(1 ...
- Oracle oerr使用
[oracle@cuug ~]$ oerr ora 01555 01555, 00000, "snapshot too old: rollback segment number %s wit ...
- Swift 提示 error running playground...
创建playground之后,我们将得到一个错误提示,Error running playground: Failed to prepare for communication with playgr ...
- centos 用户切换
在系统的/etc/.bash_profile中已经配置了各种环境变量. 用账户a登陆,ldd xxx.so查看一切链接正常. 用账户root登陆,ldd xxx.so查看一切链接正常. 用账户a登陆, ...