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)的时间,必然 ...
随机推荐
- Multi-attention Network for One Shot Learning
Multi-attention Network for One Shot Learning 2018-05-15 22:35:50 本文的贡献点在于: 1. 表明类别标签信息对 one shot l ...
- yyyy-MM-dd'T'HH:mm:ss.SSS'Z'即UTC时间,与String日期转换
本文为博主原创,未经允许不得转载: 最近在使用一个时间插件的时候,接收到的时间格式是 ’2017-11-27T03:16:03.944Z’ ,当我进行双向数据绑定的时候,由后台传过来的时间绑定到时间 ...
- JavaScript获取星期几的几种方法
星期几的4种JS代码写法,有需要的朋友可以参考一下 第一种写法 代码如下: var str = ""; var week = new Date().getDay(); if ( ...
- Use Memory Layout from Target Dialog Scatter File
参考 MDK-ARM Linker Scatter File的用法(转载) keil报错 Rebuild target 'Target 1' assembling test1.s... linking ...
- Sublime Text3 插件:DocBlockr与javascript注释规范
原:http://www.ithao123.cn/content-719950.html 1.引子 在写代码的时候,尤其是写脚本,最需要注释了.目前脚本.样式的注释格式都有一个已经成文的约定规范(这些 ...
- 1、iptables-netfilter基础
.iptables: 包过滤型防火墙功能.四表五链 .iptables规则.规则管理工具.iptables命令 .iptables链管理.规则管理.查看等 .iptables匹配条件.目标.显式扩展. ...
- _attribute_creature
生物属性控制表 comment 备注 Entry 生物ID,对就creature_template中entry Level 不等于0时改变等级为该值 Health 不等于0时改变生命值为该值 Atta ...
- Codeforces 785E. Anton and Permutation
题目链接:http://codeforces.com/problemset/problem/785/E 其实可以CDQ分治... 我们只要用一个数据结构支持单点修改,区间查询比一个数大(小)的数字有多 ...
- poi实现百万级数据导出
注意使用 SXSSFWorkbook 此类在构造表格和处理行高的时候效率极高,刚开始时我使用的 XSSFWorkbook 就出现构造表格效率极低,一万行基本需要3秒左右,那当导出百万级数据就慢的要死啦 ...
- 手动增删windows 服务和dll函数
①注册windows服务 sc create "服务名AAA" binPath= "安装目录\AAA.exe" displayname= "服务显示名 ...