LeetCode 5_Longest Palindromic Substring
LeetCode 5_Longest Palindromic Substring
题目描写叙述:
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^3))
两层循环扫描字符串的全部子串,之后推断选出的字符子串是否是回文,若是则看其长度!
代码例如以下:
class Solution {
public:
string longestPalindrome(string s)
{
// 暴力法O(n^3)
int n = s.size();
if (n == 0 || n == 1)
return s;
int maxLength = 1;
int k1 = 0, k2 = 0;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
int k = 0;
int sign = 0;
while (k < (j - i + 1)/2 && s[i + k] == s[j - k])
k++;
if(k == (j - i + 1) / 2 )
{
sign = 1;
if (j - i + 1 > maxLength)
{
maxLength = j - i + 1;
k1 = i;
k2 = j;
if (maxLength == n - i)
return s.substr(k1,k2+1);
}
}
}
}
return s.substr(k1,k2+1);
}
不用说,肯定超时。显然暴力法有非常大的优化空间。在推断子串的时候肯定有非常多反复的情况,能够用一个表记录已经推断的情况!
因为题目说能够假定字符串的长度不超过1000,所以建立一个table[1000][1000] 的bool表,初始化为false。如果某子串(如果 i 到 j )为回文。令table[ i ][ j ]为true。之后推断的时候先查表和更新表。代码例如以下:
class Solution {
public:
string longestPalindrome(string s)
{
int n = s.length();
if(n == 0 || n == 1)
return s;
int maxLength = 1;
int palindromBegin = 0;
bool table[1000][1000] = {false};
for(int i = 0; i < n; i++)
table[i][i] = true;
for (int i = 0; i < n; i++)
if(s[i] == s[i + 1])
{
table[i][i + 1] = true;
maxLength = 2;
palindromBegin = i;
}
for (int len = 3; len <= n ; len++)
{
for (int i = 0; i < n - len + 1; i++)
{
int j = i + len - 1;
if (s[i] == s[j] && table[i + 1][j - 1] == true)
{
table[i][j] = true;
maxLength = len;
palindromBegin = i;
}
}
}
return s.substr(palindromBegin, maxLength);
}
上面的方法时间复杂度为O(n^2),能够满足题目的要求。
事实上还能够考虑回文的中心点。向两边扩展(回文的中心点能够是摸个字符。也能够是某两个字符的中间),代码例如以下:
string expandAroundCenter(string s, int c1, int c2) {
int l = c1, r = c2;
int n = s.length();
while (l >= 0 && r <= n-1 && s[l] == s[r]) {
l--;
r++;
}
return s.substr(l+1, r-l-1);
}
class Solution {
public:
string longestPalindrome(string s)
{
int n = s.length();
if (n == 0) return "";
string longest = s.substr(0, 1); // a single char itself is a palindrome
for (int i = 0; i < n-1; i++) {
string p1 = expandAroundCenter(s, i, i);
if (p1.length() > longest.length())
longest = p1; string p2 = expandAroundCenter(s, i, i+1);
if (p2.length() > longest.length())
longest = p2;
}
return longest;
}
};
代码的复杂度为O(n^2)。另一种说复杂度为O(n)的方法,只是我没去看,有兴趣的能够看下: http://www.cnblogs.com/bitzhuwei/p/Longest-Palindromic-Substring-Part-II.html。
LeetCode 5_Longest Palindromic Substring的更多相关文章
- LeetCode:5_Longest Palindromic Substring | 最长的回文子串 | Medium
题目: Given a , and there exists one unique longest palindromic substring. 解题思路:1.简单思路:暴力破解法,时间复杂度O(n^ ...
- [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分析和实现
问题大意是在给定字符串中查找最长的回文子串,所谓的回文就是依据中间位置对称的字符串,比如abba,aba都是回文. 这个问题初一看,非常简单,但是会很快发现那些简单的思路都会带来O(n^3)级别的时间 ...
- 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 ...
随机推荐
- php函数 array_values()
array_values() 函数返回一个包含给定数组中所有键值的数组,但不保留键名. 提示:被返回的数组将使用数值键,从 0 开始并以 1 递增. $a=array("Name" ...
- python 端口扫描程序
#! /usr/bin/env python3 #-*- coding:utf-8 -*- import socket import threading OPEN_COUNT = 0 lock = t ...
- python 13:数字列表统计方法(min(list)、max(list)、sum(list))
numbers = list(range(1,11)) print(numbers) print(min(numbers)) #获得列表最小值 print(max(numbers)) #获得列表最大值 ...
- nodejs __dirname 与 process.cwd()的区别
var cwd = process.cwd(); console.log(cwd); console.log(__dirname); 1 2 3 cwd() 是当前执行node命令时候的文件夹地址 _ ...
- intellij IDEA常见操作
1.中文乱码设置:file - setting - Editor - File Encodings 设置为UTF-8 2.tomcat重新启动:Ctrl-F5,或者左上角 3.删除progect 先c ...
- USB 接口探测分类
USB 接口探测分类 SDP (Standand Downstream Port) 标准下行接口 标准USB都支持的接口 这种端口的D+和D-线上具有15kΩ下拉电阻.限流值如上讨论:挂起时为2.5m ...
- Eclipse安装配置——For Java
1.下载安装JRE 2.下载Eclipse,解压到相应文件夹 3.配置Eclipse 3.1 配置字体大小 -12号 3.2配置workspace默认编码,utf-8,默认系统windows 3.3 ...
- 【SQL】联合语句
一.UNION操作符 UNION 操作符用于合并两个结果集,在合并的同时去掉重复行,并按合并后结果的第一列升序排列.合并后结果集的列名由第一个结果集的列名确定. UINON连接的两个结果集必须具有相同 ...
- ORA-03113 ---end-of-file on communication channel 解决方案记录
ORALCE启动时报如下错误: ORA-03113: end-of-file on communication channel 解决方案如下: 1.查看orcle启动日志,确定具体是什么原因引 ...
- openMSP430之openmsp430-loader
openmsp430-loader This simple program allows the user to load the openMSP430 program memory with an ...