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. js 查找当前元素/this

    function findTabTitle(pageId) { var $ele = null; $(".page-tabs-content").find("a.menu ...

  2. TestLink测试管理工具的使用举例—第二篇

    本篇博客接上面TestLink测试管理工具的使用举例—第一篇的内容继续讲解如何使用TestLink工具进行测试管理. 创建一个名为“购物V1.1系统测试”的测试计划. 2.2版本管理 点击主页“测试计 ...

  3. Django-----加入MD5格式上传图片

    上传图片为什么要加 MD5 ? 答 :避免用户上传图片的时候图片名重复,而引起先上传的图片被后上传的图片所覆盖的失误! MD5是什么? 答:一种被广泛使用的密码散列函数,可以产生出一个128位(16字 ...

  4. php批量检测和去掉bom头(转)

    <?php //有些php文件由于不小心保存成了含bom头的格式而导致出现一系列的问题.以下是批量清除bom头的代码 if (isset ( $_GET ['dir'] )) { //confi ...

  5. spring boot 1.4 整合 mybatis druid

    http://www.jianshu.com/p/cef49ad91ba9spring boot 1.4 整合 mybatis druid

  6. move UVs of a texture

    Go to the material options on the left, and use a Custom UV. Create a vector parameter, use it to sc ...

  7. List进行排序

    使用Java中提供的对集合进行操作的工具类Collections,其中的sort方法,重写比较方法 /** * 根据序号对列席人进行排序 * @param lxrUser * @return */ p ...

  8. 微信支付,退款时,出现了内部错误-网站中X509Certificate2加载证书时出错

    今天给阿里云,虚拟主机 网站配置了加密证书文件,用类X509Certificate2加载证书文件时,一直报出现了内部错误,但是Demo中用控制台程序加载证书没任何问题 读取证书文件的语句: X509C ...

  9. HTML5操作麦克风获取音频数据(WAV)的一些基础技能

    基于HTML5的新特性,操作其实思路很简单. 首先通过navigator获取设备,然后通过设备监听语音数据,进行原始数据采集. 相关的案例比较多,最典型的就是链接:https://developer. ...

  10. NTP搭建

    NTP(Network Time Protocol,网络时间协议),用于同步它所有客户端时钟的服务.NTP服务器将本地系统的时钟与一个公共的NTP服务器同步然后作为时间主机提供服务,使本地网络的所有客 ...