C++ leetcode 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.
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的更多相关文章
- [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 ...
- 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 ...
- [LeetCode]Longest Palindromic Substring题解(动态规划)
Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...
- Leetcode:Longest Palindromic Substring分析和实现
问题大意是在给定字符串中查找最长的回文子串,所谓的回文就是依据中间位置对称的字符串,比如abba,aba都是回文. 这个问题初一看,非常简单,但是会很快发现那些简单的思路都会带来O(n^3)级别的时间 ...
随机推荐
- Leetcode88_Merge Sorted Array_Easy
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- Codeforces Round #441 D. Sorting the Coins(模拟)
http://codeforces.com/contest/876/problem/D 题意:题意真是难懂,就是给一串序列,第i次操作会在p[x](1<=x<=i)这些位置放上硬币,然后从 ...
- Winform 设置控件值
private void SetControlValue(Control control, object value) { try { control.FindForm().Invoke((Actio ...
- Create and format Word documents using R software and Reporters package
http://www.sthda.com/english/wiki/create-and-format-word-documents-using-r-software-and-reporters-pa ...
- Java 层序创建和遍历二叉树
直接上代码 package te.com; import java.util.LinkedList; import java.util.Queue; import java.util.logging. ...
- ps p图
1. 常用快捷键 ctrl + 单击 选中图层ctrl + j 复制出拷贝的图层 Shift+Alt+S 保存ctrl + s 保存shift + ctrl + alt + s 保存为 web 格式图 ...
- Linux Ubuntu下用Android NDK 生成独立交叉编译链
本文主要介绍使用Android NDK生成独立交叉编译链,然后使用独立交叉编译链编译Android程序 下载NDK 下载与自己操作系统相吻合的版本 下载地址 解压到安装目录(如~/myndk): ta ...
- ubuntu 安装cuda 9.1 pytorch 0.3.0
毕业再没用配过机器学习的环境了,既亲切又陌生,久违了. 系统 mint18 x64 1安装cuda 按官网提示 选的9.1版 https://developer.nvidia.com/cuda-t ...
- ACM-ICPC 2018 沈阳赛区网络预赛 J Ka Chang
Ka Chang 思路: dfs序+树状数组+分块 先dfs处理好每个节点的时间戳 对于每一层,如果这一层的节点数小于sqrt(n),那么直接按照时间戳在树状数组上更新 如果这一层节点个数大于sqrt ...
- [mybatis-spring] Transaction 事务/事务处理/事务管理器
使用mybatis-spring的主要原因之一就是: mybatis-spring允许mybatis参与到spring 事务中. mybatis-spring leverage[use (someth ...