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(n2) 超时

string longestPalindrome(string s) {
if(s.empty()) return s;
vector<vector<bool>> isPalindrome(s.length() + , vector<bool>(s.length() + , false));
string ans = s.substr(,);
for(int i = ; i <= s.length(); i++) //长度
{
for(int j = ; j <= s.length() - i; j++) //起始位置
{
if((i <= ) || (isPalindrome[j + ][i - ] && s[j] == s[j + i - ]))
{
isPalindrome[j][i] = true;
if(i > ans.length())
ans = s.substr(j, i);
}
}
}
return ans;
}

O(N)解法 AC

string longestPalindrome2(string s)
{
if(s.empty()) return s;
string ss = "#";
for(int i = ; i < s.length(); i++)
{
ss += s.substr(i, ) + "#";
}
vector<int> P(ss.length()); //记录新字符串以每一个字符为中心时 包括中心的一半长度 只有一个时长度为1
string ans = s.substr(,);
int mx = ; //向右最远的对称位置+1
int id = ; //mx对应的中心位置
for(int i = ; i < ss.length(); i++)
{
P[i] = (mx > i) ? min(P[*id - i], mx - i) : ;
while(i - P[i] >= && i + P[i] < ss.length() && ss[i - P[i]] == ss[i + P[i]]) P[i]++;
if(P[i] - > ans.length())
ans = s.substr((i - P[i] + )/, P[i] - );
if(i + P[i] > mx)
{
mx = i + P[i];
id = i;
}
}
return ans;
}

【leetcode】Longest Palindromic Substring (middle) 经典的更多相关文章

  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. C++ leetcode Longest Palindromic Substring

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

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

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

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

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

  7. Leetcode: Longest Palindromic Substring. java

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

  8. LeetCode——Longest Palindromic Substring

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

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

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

随机推荐

  1. Convert Windows 32bit dirver to Windows 64bit

    Pre-condition: 1.source code(vc6.0+WDK based) Development environment: 2.VS2013 3.WDK 8/8.1 Steps: 1 ...

  2. 使用gulp脚本配合TypeScript开发

    目标:编写TypeScript时,保存即生成js文件.   使用npm安装以下组件 gulp gulp-rename through-gulp gulp-typescript   编写gulpfile ...

  3. CoreAnimation 核心动画二 锚点

    锚点: anchorPoint     以锚点为中心 执行动画 (与 渔夫固定船的点时一致的) anchorPoint 默认是 0.5,0.5  (注意: 锚点 是一个比例) anchorPoint ...

  4. C#对象XML序列化

    1.Xml序列化操作类 .Net Framework提供了对应的System.Xml.Seriazliation.XmlSerializer负责把对象序列化到XML,和从XML中反序列化为对象. 以下 ...

  5. javascript 笔记——bind 用法小技巧

    $(function(){ //代码阅读能力 function speak(name){ return "Hello " + name; } //此处写代码 //方法一 //重点是 ...

  6. windbg基本命令

    1, .reload k 当前调用堆栈.u 当前正在执行的代码. 2, ~ 查看被调试进程中的线程信息每一行是一个线程的信息.第一行中,0 表示这个进程的编号:1ff4.1038 是 16 进制数字, ...

  7. 十天学会零基础入门学习Photoshop课程(在线观看)

    适合人群:在校学生 在职工作者 淘宝运营者等一系列会操作电脑的人群 课程目录 试学课 课时11前言 8分钟1秒 课时22工作界面 试学课 课时33文件的新建 试学课 课时44文档保存 11分钟24秒  ...

  8. Mysql配置文件my.cnf解析

    # vim /etc/my.cnf [client] port = 3306 //客户端所连接的端口号 socket = /tmp/mysql.sock //客户端所连接的sock文件存放位置 [my ...

  9. 更新Android SDK 访问谷歌等无需代理方法

    最近要做ANDROID,本来是想通过找镜像网址下载,发现公司网络屏蔽了,后来网络上搜索一圈,发现如下方法 1)更改HOST 2)使用代理 使用代理在公司的环境中属于违规操作,因此不能使用 只剩更改HO ...

  10. get post

    浅谈HTTP中Get与Post的区别 2009-03-31 14:51 by hyddd, 248341 阅读, 74 评论, 收藏, 编辑 Http定义了与服务器交互的不同方法,最基本的方法有4种, ...