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.

这里给出一种AC的动态规划解法:时间和空间复杂度O(n2)

f(i,j)表示范围在(i,j)的串是否在回文串,同时记录最大回文串长度和起始位置
f(i,j)=(s[i]==s[j]and(i−j<2orf[i+1][j−1]))i−j>=2
f[i][i]=true;
class Solution {
public:
string longestPalindrome(string s) {
const int n=s.size();
bool f[n][n];
fill_n(&f[0][0],n*n,false);
size_t max_len = 1,start =0;
for(size_t i =0;i<s.size();++i){
f[i][i]=true;
for(size_t j=0;j<i;++j){
f[j][i]=(s[j]==s[i]&&(i-j<2||f[j+1][i-1]));
if(f[j][i]&&max_len<(i-j+1))
{
max_len = i-j+1;
start = j;
}
}
}
return s.substr(start,max_len);
}
};

24-Longest Palindromic Substring-Leetcode的更多相关文章

  1. Longest Palindromic Substring——LeetCode

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

  2. Longest Palindromic Substring -LeetCode

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

  3. Longest Palindromic Substring leetcode java

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

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

    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 lengt ...

  6. 求最长回文子串 - leetcode 5. Longest Palindromic Substring

    写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...

  7. LeetCode 5 Longest Palindromic Substring(最长子序列)

    题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...

  8. 【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 ...

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

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

  10. leetcode:Longest Palindromic Substring(求最大的回文字符串)

    Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

随机推荐

  1. BUAA_2020_软件工程_热身作业

    项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任建) 这个作业的要求在哪里 热身作业要求 我在这个课程的目标 了解软件工程的技术,掌握工程化开发的能力 这个作业在哪个具体方面 ...

  2. elasticsearch的dsl查询

    测试es的dsl查询,准备数据,在插入数据的时候,如果index.type.mapping都没有,es会自动创建 一.数据的准备 curl -XPOST "http://192.168.99 ...

  3. 认真讲说static关键字

    static 关键字主要有以下四种使用场景 修饰成员变量和成员方法 静态代码块 修饰类(只能修饰内部类) 静态导包(用来导入类中的静态资源,1.5之后的新特性) 修饰成员变量和成员方法(常用) 被 s ...

  4. EFCore_环境搭建与简单使用_01

    开发环境搭建 经典步骤:建实体类.建DbContext.生成数据库 本次使用codefirst模式,走下流程,(你也可以先建好数据库,用命令行的形式,直接生成DbContext,而且生成的DbCont ...

  5. SpringBoot整合Easyexcel操作Excel,闲暇之余,让我们学习更多

    关于封面:晚饭后回自习室的路上 Easyexcel 官方文档 Easyexcel | github 前言 最近也是在写的一个小练习中,需要用到这个.趁着这次就将写个整合的Demo给大家. 希望能够让大 ...

  6. HttpContext.Current.Request.Url 地址:获取域名

    假设当前页完整地址是:http://www.test.com/aaa/bbb.aspx?id=5&name=kelli 协议名----http://域名  ---- www.test.com站 ...

  7. python 模块 hashlib(提供多个不同的加密算法)

    hashlib是涉及安全散列和消息摘要,提供多个不同的加密算法借口,如SHA1.SHA224.SHA256.SHA384.SHA512.MD5等. import hashlib m = hashlib ...

  8. robot_framewok自动化测试--(7)认识RIDE

    认识 RIDE RIDE 作为 Robot Framework 的"脸面",虽然我们已经可以拿它来创建和运行测试了,但我们对它的认识并不全面,这一小节我们将了解这个工具的使用. 1 ...

  9. 设计模式学习-使用go实现代理模式

    代理模式 定义 优点 缺点 应用场景 代码实现 参考 代理模式 定义 定义:为其对象提供一种代理以控制这个对象的访问.通俗点讲就是它在不改变原始类(或叫被代理类)代码的情况下,通过引入代理类来给原始类 ...

  10. SpringCloud升级之路2020.0.x版-32. 改进负载均衡算法

    本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 在前面一节,我们梳理了实现 Feign 断路器以及线程隔离的思路,这一节,我们先不看如何源 ...