leetcode-algorithms-5 Longest Palindromic Substring
leetcode-algorithms-5 Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd"
Output: "bb"
解法1
暴力破解,从最长的长度开始查找所有的子串,检查子串是否为回文串.
class Solution
{
public:
bool checkPalindrome(const std::string &s)
{
int len = s.size();
for (int i = 0; i < len/2; ++i)
{
if (s[i] != s[len -i - 1])
return false;
}
return true;
}
string longestPalindrome(string s)
{
std::string pal;
int len = s.size();
for (int i = len; i > 0; --i)
{
for (int start = 0; start + i <= len; ++start)
{
pal = s.substr(start, i);
if (checkPalindrome(pal))
return pal;
}
}
return pal;
}
};
时间复杂度: O(n^3).三个嵌套循环,O(n) * O(n) * O(n/2) = O(n^3).
空间复杂度: O(1)
解法2
观察回文串的规律.如:aba,先找到一个字母,然后两边都增加相同字母也是回文串,以此类推,就可以找到最大回文串.还有另种情况是:abba,则要先找到两个相邻是相同的字母.因此关键就是找到这个回文串的中心.
class Solution
{
public:
int palindromeCenter(const std::string &s, int left, int right)
{
while(left >= 0 && right < s.size() && s[left] == s[right])
{
--left;
++right;
}
return right - left - 1;
}
string longestPalindrome(string s) {
int len = s.size();
if (len <= 0) return "";
int start = 0;
int palindromeLen = 0;
for (int i = 0; i < len; ++i)
{
int len1 = palindromeCenter(s, i, i);
int len2 = palindromeCenter(s, i, i + 1);
int maxLen = len1 > len2 ? len1 : len2;
if (maxLen > palindromeLen)
{
start = i - (maxLen - 1) / 2;
palindromeLen = maxLen;
}
}
return s.substr(start,palindromeLen);
}
};
时间复杂度: O(n^2).
空间复杂度: O(1).
leetcode-algorithms-5 Longest Palindromic Substring的更多相关文章
- 【一天一道LeetCode】#5 Longest Palindromic Substring
一天一道LeetCode系列 (一)题目 Given a string S, find the longest palindromic substring in S. You may assume t ...
- 【LeetCode OJ】Longest Palindromic Substring
题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- LeetCode(3)题解: Longest Palindromic Substring
https://leetcode.com/problems/longest-palindromic-substring/ 题目: Given a string S, find the longest ...
- 【LeetCode】5. Longest Palindromic Substring 最长回文子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...
- 【LeetCode】5. Longest Palindromic Substring 最大回文子串
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- 【leetcode】5. Longest Palindromic Substring
题目描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- leetcode题解 5. Longest Palindromic Substring
题目: Given a string s, find the longest palindromic substring in s. You may assume that the maximum l ...
- LeetCode OJ:Longest Palindromic Substring(最长的回文字串)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【LeetCode】005. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- LeetCode(5) - Longest Palindromic Substring
这道题要求的是给你一个string, 如“adcdabcdcba",要求返回长度最大的回文子字符串.这里有两个条件,一是子字符串,而是回文.用纯暴力搜索的话,需要用到O(n^3)的时间,必然 ...
随机推荐
- Even Odds (java)
从1到n的奇数,从1到n之间的偶数,排列在一起,找到第k个数 Input 输入包含 n and k (1 ≤ k ≤ n ≤ 1012). Please, do not use the %lld sp ...
- 剥开比原看代码11:比原是如何通过接口/create-account创建帐户的
作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...
- Collection与Collections的区别
Collection是集合类的上级接口,继承与他有关的接口主要有List和Set Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索.排序.线程安全等操作 稍微举 ...
- ZOJ 3632 ----dp+优先队列
上个礼拜学长讲了优先队列的说.... emmmmmm.... 看着题解敲了一题...先m下. #include<cstring> #include<algorithm> #in ...
- MS-Windows中的Git命令行
Git command line for MS-Windows Inhalt 1 Download and install, or copy the git command line suite fo ...
- 操作 html 的时候是使用 dom 方法还是字符串拼接?
比如一个列表里面有很多个 li,要给他们加上数据.但多少个 li 是不确定的,由后台数据确定.这时候,就要动态生成 html 内容了. 那么,这个过程, 是使用 += 方法把标签.数据进行一个个的字符 ...
- <aop:aspect>与<aop:advisor>的区别
在开发过程中,不少有Spring Aop的使用,在面向切面编程时,我们会使用< aop:aspect>:在进行事务管理时,我们会使用< aop:advisor>.那么,对于&l ...
- RN原生调用一:安卓Toast
首先安卓原生方法:Toast.makeText(getApplicationContext(), "默认的Toast", Toast.LENGTH_SHORT); 在RN中js如何 ...
- Mysql一些常用语句
1.查询表创建的时间: SELECT CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERETABLE_NAME='TableName'
- 分组查询以及having使用
Group by 注意事项:对查询的列有限制,除了聚合函数外,就必须是分组的项 order by总是放在最后 代码示例: select Gender as 性别 ,count (Gender) a ...