public class Solution {
private int lo, maxLen; public String LongestPalindrome(String s)
{
int len = s.Length;
if (len < )
return s; for (int i = ; i < len - ; i++)
{
extendPalindrome(s, i, i); //assume odd length, try to extend Palindrome as possible
extendPalindrome(s, i, i + ); //assume even length.
}
return s.Substring(lo, maxLen);
} private void extendPalindrome(String s, int j, int k)
{
while (j >= && k < s.Length && s[j] == s[k])
{
j--;
k++;
}
if (maxLen < k - j - )
{
lo = j + ;
maxLen = k - j - ;
}
}
}

https://leetcode.com/problems/longest-palindromic-substring/#/description

补充一个python的实现,使用动态规划解决:

 class Solution:
def longestPalindrome(self, s: 'str') -> 'str':
n = len(s)
dp = [[False for _ in range(n)]for _ in range(n)]
count =
res = ''
for i in range(n):
dp[i][i] = True
if count < :
res = s[i]
count = for i in range(n-):
if s[i] == s[i+]:
dp[i][i+] = True
if count < :
res = s[i:i+]
count = for k in range(,n+):
for i in range(n-k+):
j = i + k -
if s[i] == s[j] and dp[i+][j-]:
dp[i][j] = True
if count < j + - i:
count = j + -i
res = s[i:j+]
return res

leetcode5的更多相关文章

  1. leetcode--5. Longest Palindromic Substring

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

  2. Leetcode5:Longest Palindromic Substring@Python

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

  3. LeetCode5:Longest Palindromic Substring

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

  4. leetcode-5 最长回文子串(动态规划)

    题目要求: * 给定字符串,求解最长回文子串 * 字符串最长为1000 * 存在独一无二的最长回文字符串 求解思路: * 回文字符串的子串也是回文,比如P[i,j](表示以i开始以j结束的子串)是回文 ...

  5. LeetCode5 Longest Palindromic Substring

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

  6. leetcode5 Implement strstr() 实现strstr函数功能

    Implement strstr() 实现strstr函数功能 whowhoha@outlook.com Question: Implement strstr(). Returns the index ...

  7. LeetCode5. Longest Palindromic Substring 最长回文子串 4种方法

    题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题意很简单,就是求一个字符串得最长子串,这里的子串指连续的. 本文给 ...

  8. [Swift]LeetCode5. 最长回文子串 | Longest Palindromic Substring

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

  9. leetcode5:最长回文子串

    给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &quo ...

随机推荐

  1. vscode 完全支持zeng code的写法

    一.快速编写HTML代码 1.  初始化 HTML文档需要包含一些固定的标签,比如<html>.<head>.<body>等,现在你只需要1秒钟就可以输入这些标签. ...

  2. 剑指Offer 52. 正则表达式匹配 (字符串)

    题目描述 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹配整个模式 ...

  3. ssh+key

    一.ssh概述 在进行数据传输之前,SSH先对联机数据包通过加密技术进行加密处理,加密后在进行数据传输.确保了传递的数据安全. lrzsz安装包传输本地与Linux SSH客户端(ssh命令)还包含一 ...

  4. 读取Excel表格中数据原型

    写下这篇博客来记录自己的工作,这部分功能是读取Excel表格中的数据,并通过c#中的datagridview控件将读取的数据显示出来.为了方便用户,我设计了一个read按钮,用户点击这个按钮,会弹出打 ...

  5. 王者荣耀交流协会第5次Scrum立会

    开会时间:2017年10月31日下午18:00-18:31  共计31分钟 开会地点:一食堂二楼靠近窗户倒数第四排 今日完成工作进度: 王超同学完成了将生成饼状图原型整合到程序中 立会内容: 添加了新 ...

  6. 【软件安装与环境配置】TX2刷机过程

    前言 使用TX2板子之前需要进行刷机,一般都是按照官网教程的步骤刷机,无奈买不起的宝宝只有TX2核心板,其他外设自己搭建,所以只能重新制作镜像,使用该镜像进行刷机. 系统需求 1.Host Platf ...

  7. Linux学习man page

    1.3.8需要记住,分别代表用户操作.函数库和管理指令. ##man -f command 显示出命令的所有说明文档.等同于 whatis ##man -k key 显示出带key的所有说明文档.等同 ...

  8. git全局忽略文件

    mac系统如何显示和隐藏文件 创建 ~/.gitignore_global .DS_Store __pycache__/ 配置选项 git config --global core.excludesf ...

  9. 使用vue自定义简单的消息提示框

    <style scoped> /** 弹窗动画*/ a { text-decoration: none } .drop-enter-active { /* 动画进入过程:0.5s */ t ...

  10. goaccess iis w3c 自定义log 格式参考

    goaccess 支持强大的自定义log 格式,比如我们需要分析iis w3c 格式日志 参考iis w3c 字段 date time s-ip cs-method cs-uri-stem cs-ur ...