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 ...
随机推荐
- masonry使用介绍
Masonry使用介绍 下面是Masonry的代码地址:https://github.com/Masonry/Masonry 介绍一个简单使用: <pre><code>[vie ...
- 如何设置jsp默认的编码为utf-8
方法一: 文件里写: <%@ page contentType="text/html; charset=UTF-8" %> 方法二: 选择window –> P ...
- [转] Are You Making a Big Mistake in This Volatile Market?
Stock market volatility continues unabated. It may be too early to tell, but I’m marking the top of ...
- GDB调试技巧
1. 查看内存分布 (gdb) info proc mappings 2. 对于类的调试,先通过行号来设断点, 比如:(gdb) b TcpConnection.cc:63 3. 打印数组的内容 (g ...
- iOS 独立开发记录(上)
个月前,完成了个人App的2.0版本,也在普天同庆的六一儿童节这天上架了.因为是个人开发,很多实现都是边探索边做.现在完成之后再回顾,发现自己走了些弯路.所以写了这篇总结,概览了从想法.设计.开发到最 ...
- spring定时器任务多任务串行执行问题排查
最近发现个生产问题,定时器任务某些任务没有及时执行.经过研究排查发现spring 定时器任务scheduled-tasks默认配置是单线程串行执行的,这就造成了若某个任务执行时间过长,其他任务一直在排 ...
- Linux shell入门基础(四)
四.进程优先级前台后台 01.进程控制 #find /name aaa & #ps aux | grep find #updatedb & #ps aux | grep update ...
- 什么是mimeType?
因特网多媒体邮件扩展标示内容是什么格式.告诉浏览器或者server如何解析该数据http的请求和相应都含有一个mimeType字段 =>content-type(通用首部)
- Java并发编程之CAS
CAS(Compare and swap)比较和替换是设计并发算法时用到的一种技术.简单来说,比较和替换是使用一个期望值和一个变量的当前值进行比较,如果当前变量的值与我们期望的值相等,就使用一个新值替 ...
- 给出2n+1个数,其中有2n个数出现过两次,如何用最简便的方法找出里面只出现了一次的那个数(转载)
有2n+1个数,其中有2n个数出现过两次,找出其中只出现一次的数 例如这样一组数3,3,1,2,4,2,5,5,4,其中只有1出现了1次,其他都是出现了2次,如何找出其中的1? 最简便的方法是使用异或 ...