leetcode5
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的更多相关文章
- leetcode--5. Longest Palindromic Substring
题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- Leetcode5:Longest Palindromic Substring@Python
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- LeetCode5:Longest Palindromic Substring
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- leetcode-5 最长回文子串(动态规划)
题目要求: * 给定字符串,求解最长回文子串 * 字符串最长为1000 * 存在独一无二的最长回文字符串 求解思路: * 回文字符串的子串也是回文,比如P[i,j](表示以i开始以j结束的子串)是回文 ...
- LeetCode5 Longest Palindromic Substring
题意: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- leetcode5 Implement strstr() 实现strstr函数功能
Implement strstr() 实现strstr函数功能 whowhoha@outlook.com Question: Implement strstr(). Returns the index ...
- LeetCode5. Longest Palindromic Substring 最长回文子串 4种方法
题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题意很简单,就是求一个字符串得最长子串,这里的子串指连续的. 本文给 ...
- [Swift]LeetCode5. 最长回文子串 | Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- leetcode5:最长回文子串
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &quo ...
随机推荐
- python+appium 自动化1--启动手机京东app
出处:https://www.cnblogs.com/yoyoketang/p/6128735.html 前言: 环境搭建好了.接下来先体验下如何启动app--1.首先获取包名:2.然后获取launc ...
- 基础、hibernate目前应用的对比
* ***************************hibernate** ***************************** 一.导包 mysql二.在默认src下创建hibern ...
- .net ORM框架(Dapper简单应用)
1.引入 Dapper.dll类库 2.创建书籍模型book using System; using System.Collections.Generic; using System.Linq; us ...
- 一篇提及如何通过串口读取并提取GPS信号的论文
一篇提及如何通过串口读取并提取GPS信号的论文 作者:崔杰 梁计春 王国军 目前,在用计算机进行数据传输时,常用的是串行通信方式.在Visual C++的编程中,既可以用Windows API函数进行 ...
- __FILES__
_FILE_ :被称为PHP魔术常量 ,返回当前执行PHP脚本的完整路径和文件名,包含一个绝对路径 1)dirname(__FILE___) 函数返回的是脚本所在在的路径. 比如文件 b.php ...
- sed语法
Sed 命令行 以下是我们可以指定单引号在命令行sed命令的格式如下: sed [-n] [-e] 'command(s)' files 例子 考虑一下我们有一个文本文件books.txt待处理,它有 ...
- Linux 驱动——Led驱动1
led_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/init ...
- 自动化测试-4.selenium的xpath定位
前言 在上一篇简单的介绍了用工具查看目标元素的xpath地址,工具查看比较死板,不够灵活,有时候直接复制粘贴会定位不到.这个时候就需要自己手动的去写xpath了,这一篇详细讲解xpath的一些语法. ...
- pycharm中添加PATH变量
最近在pycharm中run程序,终端terminal没有问题,在pycharm找不到$PATH中的变量值,如下图所示 同样的命令,在终端敲就没毛病,终端echo $PATH的时候,显示的是有cuda ...
- configparse模块和hashlib模块
# import configparser # # config = configparser.ConfigParser() #config = {} # config['DEFAULT'] = {' ...