明天就要上课了,再过几天又要见班主任汇报项目进程了,什么都没做的我竟然有一种迷之淡定,大概是想体验一波熬夜修仙的快乐了。不管怎么说,每天还是要水一篇博文,写一个LeetCode的题才圆满。

题目:Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example:  Input: "babad" ;  Output: "bab"; Note: "aba" is also a valid answer.

第一想法是遍历的过程用堆栈做,想了半天觉得脑子一片空白,放弃,之后看了solution,提到了动态规划,有了想法。太久没刷题了。。。

class Solution {
public:
string longestPalindrome(string s) {
bool flags[s.size()][s.size()]; //flags[i][j]为true,表示s[i]到s[j]位回文子串
for(int i =;i<s.size();i++){
for(int j = ; j< s.size();j++){
flags[i][j] = (i==j )? true:( j == i+? (s[i]==s[j]):false);
}
}
int longest = ; //标记最长回文子串的长度
int start = ; //标记最长回文子串的起始位置
for(int i = ; i<s.size()-;i++){ //寻找有没有bb这样的回文串,然后更新longest和start
if (flags[i][i+] == true){
longest = ;
start = i;
break;
}
}
for(int i = s.size()-; i>=;i--){
for (int j = i+; j < s.size(); j++){
if(s[i]==s[j]&&flags[i+][j-]==true){
flags[i][j] = true;
longest = longest>(j-i+)?longest:(j-i+);
start = longest>(j-i+)?start:i;
}
}
}
return s.substr(start,longest);
}
};

提交之后96ms,击败20%。这么慢,我要哭了。O(n2)的时间复杂度和空间复杂度。继续看solution。。。

从中心扩充:这个解法时间复杂度为O(n2),但是空间复杂度为O(1)

每一个回文子串都含有一个中心,因此一个长度为n的字符串,含有2n-1个回文子串的中心,因为回文子串的中心可以是字母,可以是空白,比如aba、aa。

出去浪了一把,代码没写完,明天补上~不能保卫祖国,就好好学习报效祖国吧。

好了,今天来补代码吧。23ms,击败了42%。写代码的时候干了一件傻事,每次找到回文子串要更新三个参数,因为三个参数不是一开始就设置好的,最后加了一个参数flag,想都没想直接在longest之后添加了flag的更新,导致flag不正确,结果错误。这个逻辑错误也是我经常犯的,今后一定要注意参数的更新顺序是否正确。

class Solution {
public:
string longestPalindrome(string s) {
int longest = 0; //回文中心到边缘的长度
int center = 0;
int i = 1;
bool flag = true; //标记回文子串的中心是字母还是空,true为字母
while(i<=s.size()-1){
int j = 1,now = 0;
while(i-j>=0 && s[i+j] == s[i-j] && i+j <s.size()){ //寻找类似于ABA的回文子串
now++;
j++;
}
center = now>=longest?i:center;
       flag = now>=longest?true:flag;
longest = now>=longest?now:longest; //now == longest的时候也要更新子串,因为aba和aa的longest都为1,但是aba比aa长 j = 1;
now = 0;
while(s[i-j]==s[i+j-1] && i-j>=0 && i+j-1<s.size()){ //寻找类似于AA这样的回文子串
now++;
j++;
}
center = now>longest?i:center;
       flag = now>longest?false:flag;
longest = now>longest?now:longest;
i++;
}
return flag?s.substr(center-longest,2*longest+1):s.substr(center-longest,2*longest);
}
};

  看了3ms的答案,发现了一个小trick,就是i的更新不用每次都+1,可以直接把i,也就是回文子串的中心更新成回文子串的最右边,因为不存在以原回文子串的中心到右边的任一位置为中心的回文子串长度长与原子串。还有可以用start和len来代替center、longest、flag三个参数,start表示回文开始,len代表回文长度。

C++ leetcode Longest Palindromic Substring的更多相关文章

  1. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  2. Leetcode Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  3. [LeetCode] Longest Palindromic Substring(manacher algorithm)

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  4. Leetcode: Longest Palindromic Substring && Summary: Palindrome

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  5. LeetCode:Longest Palindromic Substring 最长回文子串

    题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  6. Leetcode: Longest Palindromic Substring. java

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  7. LeetCode——Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  8. [LeetCode]Longest Palindromic Substring题解(动态规划)

    Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...

  9. Leetcode:Longest Palindromic Substring分析和实现

    问题大意是在给定字符串中查找最长的回文子串,所谓的回文就是依据中间位置对称的字符串,比如abba,aba都是回文. 这个问题初一看,非常简单,但是会很快发现那些简单的思路都会带来O(n^3)级别的时间 ...

随机推荐

  1. MyBatis数据库测试代码自动生成

    <!-- generatorConfig.xml配置,其中:<plugin type="org.mybatis.generator.plugins.ToStringPlugin& ...

  2. gulp的使用介绍及技巧

    gulp的使用介绍及技巧 转载: https://www.cnblogs.com/2050/p/4198792.html 1.gulp的安装 首先确保你已经正确安装了nodejs环境.然后以全局方式安 ...

  3. 【Java】【反射】

    一,java的核心机制 java有两种核心机制:java虚拟机(JavaVirtual Machine)与垃圾收集机制(Garbage collection): Java虚拟机:是运行所有Java程序 ...

  4. 添加删除tag

    为某个分支打tag git tag -a v1. 9fceb02 删除tag git tag -d test_tag git push origin --delete tag test_tag 推送: ...

  5. CentOS7 安装Perl

    官网:http://www.cpan.org/src/ wget https:.tar.gz cd perl- ./Configure -des -Dprefix=$HOME/localperl ma ...

  6. git创建、删除分支

    //创建分支 git branch branchname //创建并切换到新分支 git checkout -b branchname //远程分支 git push origin branchnam ...

  7. Linux下解包/打包,压缩/解压命令

    .tar 解包:tar xvf FileName.tar 打包:tar cvf fileName.tar DirName tar.gz和.tgz 解压:tar zxvf FileName.tar.zi ...

  8. "不是内部或外部命令"

    问题描述: 使用cmd 运行某个路径下(一般是C:PROGRAM FILES\...或者E:\program files\....或者D:\program files\......下面的某个)的exe ...

  9. JDK中关于BIO,NIO,AIO,同步,异步介绍

    在理解什么是BIO,NIO,AIO之前,我们首先需要了解什么是同步,异步,阻塞,非阻塞.假如我们现在要去银行取钱: 同步 : 自己亲自出马持银行卡到银行取钱(使用同步IO时,Java自己处理IO读写) ...

  10. DAY5 基本数据类型及内置方法

    一.可变与不可变数据类型 1.可变类型:值改变,但是id不变,证明就是在改变原值,是可变类型 2.不可变类型:值改变,但是id也跟着变,证明是产生了新的值,是不可变类型 二.数字类型 1.整型int ...