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. python接口自动化测试(一)-request模块

    urllib.request模块是python3针对处理url的. 1. 首先导入: from urllib import request 2. 构造url,构造url的headers信息和传参[re ...

  2. Apache Flume 学习笔记

    # 从http://flume.apache.org/download.html 下载flume ############################################# # 概述: ...

  3. Hadoop学习笔记02_MapReduce练习

    搭建好环境之后 ,就来跑个简单的Mapreduce试试看吧.这个比第一课难多了,需要多多练习并熟练掌握. 需要编写py脚本以及shell脚本, 所以需要学习Python和Linux的Shell编程. ...

  4. 三、fgetc与fputc

    fgetc 功能:从流中读取一个字符 原型:int fgetc(FILE *stream); 参数: stream:要读取的流指针 返回:读取到的字符,如果读完则返回EOF,EOF是end of fi ...

  5. setfacl语法

    1.setfacl的用途setfacl命令可以用来细分linux下的文件权限. chmod命令可以把文件权限分为u,g,o三个组,而setfacl可以对每一个文件或目录设置更精确的文件权限. 换句话说 ...

  6. locate语法

    1.命令格式:locate [参数] [文件] 2.命令功能:locate命令可以在搜寻数据库时快速找到档案,数据库由updatedb程序来更新,updatedb是由cron daemon周期性建立的 ...

  7. maven项目pom.xml第一行报错

    maven项目pom.xml第一行报错 这是第一行:<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi= ...

  8. 几个特殊的IP地址

    1)私有地址     IP地址在全世界范围内唯一,看到这句话你可能有这样的疑问,像192.168.0.1这样的地址在许多地方都能看到,并不唯一,这是为何?Internet管理委员会规定如下地址段为私有 ...

  9. 整理面试问题iOS

    1.如何添加手势操作. 我们以在view上来举例 //创建一个view UIView *tapView=[UIView new]; tapView.frame=CGRectMake(, , kWidt ...

  10. idea的maven项目下spring与mybatis整合

    两周前学习mybatis框架,参考了网上多位大神的博客,但因为各种原因(不解释)总是没法成功搭建环境并运行项目.周末花了点时间阅读了文档并整理之前琐碎的内容,解决掉之前遇到的问题.现将整合环境的关键步 ...