LeetCode刷题-005最长回文子串
给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为1000。
示例 1:
输入: "babad"
输出: "bab"
注意: "aba"也是一个有效答案。
示例 2:
输入: "cbbd"
输出: "bb"
string longestPalindrome(string s)
{ string str = "$#";
for(int i = ; i < s.size(); i++) //字符串长度奇偶变换,添加'$'防止数组越界
{
str += s[i];
str += "#";
}
str += "$";
//cout<<str; int p[str.size()-]={}; //定义p[i]数组 int right = , mid = ;
for(int i = ; i < str.size()-; i++)
{
if(right > i)
{
p[i] = (p[*mid - i] < (right - i) ? p[*mid - i] : (right - i)); //计算以i为中心的回文字串的最小长度
}
else
{
p[i] = ;
} while(str[i - p[i]] == str[i + p[i]]) p[i]++; //回文子串长度延伸 if(i + p[i] > right)
{
right = i + p[i];
mid = i;
} } int max = , middle=;
for(int i = ; i < str.size()-; i++) //生成最终结果
{
if(p[i] > max)
{
middle = i;
max = p[i];
}
}
LeetCode刷题-005最长回文子串的更多相关文章
- leetcode刷题五<最长回文子串>
下面是题目的描述 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 . 示例 : 输入: "babad" 输出: "bab" 注意: ...
- LeetCode随缘刷题之最长回文子串
这一题我用的相对比较笨的方法. 相对于大佬们用的动态规划法,比较复杂.但却更容易理解,我主要是通过记录下标来确定最长回文串的. package leetcode.day_12_06; /** * 给你 ...
- C#LeetCode刷题之#409-最长回文串(Longest Palindrome)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3788 访问. 给定一个包含大写字母和小写字母的字符串,找到通过这 ...
- LeetCode之“字符串”:最长回文子串
题目要求: 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串.例如,给出字符串 "abcdzdcab",它的最长回文子串为 & ...
- Leetcode(5)-最长回文子串(包含动态规划以及Manacher算法)
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &quo ...
- LeetCode:Longest Palindromic Substring 最长回文子串
题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- 转载:LeetCode:5Longest Palindromic Substring 最长回文子串
本文转自:http://www.cnblogs.com/TenosDoIt/p/3675788.html 题目链接 Given a string S, find the longest palindr ...
- Leetcode(5)最长回文子串
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...
- leecode第五题(最长回文子串)
class Solution { public: string longestPalindrome(string s) { int len = s.length(); || len == ) retu ...
随机推荐
- storm ui 网页一直出现提示loading summary
在更换了一次storm的版本之后:访问 http://mini1:8080/index.html 来查看storm的运行情况,但是出现了网页一直出现提示loading summary,但是通过透明的弹 ...
- dicom错误解决
https://github.com/pydicom/pydicom/issues/331 sudo apt-get install python-gdcm
- Host Only、NAT和Bridge三种网络连接
Host Only.NAT和Bridge三种网络连接 在安装好了Linux镜像之后,如何连接物理机和虚拟机呢?这就需要网络连接,网络连接有三种:HostOnly.NAT.Bridge,它们都可用于Gu ...
- JavaScript判断对象是否是NULL
这个方法是我踩了很多坑之后找到的,对数组等类型的对象都很好使,果断收藏! function isEmpty(obj) { // 检验 undefined 和 null if (!obj &&a ...
- storybook实践
很久之前就听说过storybook,一直想实践一下
- Tomcat FAIL - Deploy Upload Failed, Exception: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (110960596) exceeds the confi
https://maxrohde.com/2011/04/27/large-war-file-cannot-be-deployed-in-tomcat-7/ Go to the web.xml of ...
- powershell 常用命令之取磁盘分区信息
//查看mac 地址 PS C:\Users\yyy> get-wmiobject -class Win32_NetworkAdapterConfiguration -namespace &qu ...
- TCP/IP协议、UDP协议、 Http协议
开放式系统互联通信参考模型(Open System Interconnection Reference Model,缩写为 OSI),简称为OSI模型(OSI model),一种概念模型,由国际标准化 ...
- [SMB share]Create SMB share under powershell / poweshell下创建本机的SMB共享
New-SmbShare -Name share-name -Path C:\share -FolderEnumerationMode AccessBased -CachingMode Documen ...
- 镜像站nginx
server { listen 80 default_server; charset utf-8; server_name monitor.autoai.com; access_log /srv/lo ...