LeetCode之“字符串”:最短回文子串
题目要求:
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.
For example:
Given "aacecaaa", return "aaacecaaa".
Given "abcd", return "dcbabcd".
这道题用暴力破解是无法通过LeetCode上的测试的。
1. 法一(暴力破解)
超过时间!!!!
int findCenter(string s)
{
int center = ;
int szS = s.size();
for (int i = ; i < szS; i++)
{
int k = ;
while (k <= i && k < szS - i)
{
if (s[i - k] != s[i + k])
break;
k++;
}
if (i - k == - && i > center)
center = i;
}
return center;
} string char2String(char c)
{
stringstream ss;
string s;
ss << c;
ss >> s;
return s;
} string shortestPalindrome(string s) {
// clear spaces
if (s.front() == ' ')
s.erase(, );
if (s.back() == ' ')
s.pop_back(); string ret;
int szS = s.size();
if (szS == )
ret = "";
else if (szS == )
{
s += s;
ret = s;
}
else
{
ret = s;
int center = findCenter(s);
int left = center;
int right = center + left + ;
if (szS - center - > center && s[center] == s[center + ])
{
right = szS;
}
for (int i = right; i < szS; i++)
{
ret.insert(, char2String(s[i]));
}
} return ret;
}
2. 法二(暴力破解)
法二思路来源:How to get the shortest palindrome of a string。重要语句摘录如下:
Just append the reverse of initial substrings of the string, from shortest to longest, to the string until you have a palindrome. e.g., for "acbab", try appending "a" which yields "acbaba", which is not a palindrome, then try appending "ac" reversed, yielding "acbabca" which is a palindrome.
法二相对法一来说更加简单容易理解,但同样超过时间!!!!
bool isPalindrome(string s)
{
int szS = s.size();
for (int i = ; i < szS / ; i++)
{
if (s[i] != s[szS - i - ])
return false;
}
return true;
} string shortestPalindrome(string s)
{
// clear spaces
if (s.front() == ' ')
s.erase(, );
if (s.back() == ' ')
s.pop_back();
//
string ret;
int szS = s.size();
if (szS == )
{
ret = "";
}
else if (szS == )
{
s += s;
ret = s;
}
else if (isPalindrome(s))
{
ret = s;
}
else
{
for (int i = ; i < szS; i++)
{
string tmpStr = s;
for (int j = szS - i - ; j < szS; j++)
tmpStr.insert(tmpStr.begin(), s[j]); if (isPalindrome(tmpStr))
{
ret = tmpStr;
break;
}
}
}
return ret;
}
看来只能另寻他法了。。。。
3. 法三
法三参考自C++ 8 ms KMP-based O(n) time & O(n) memory solution,利用了KMP的思想。具体程序如下:
class Solution {
public:
string shortestPalindrome(string s)
{
string rev_s(s);
reverse(rev_s.begin(), rev_s.end());
string combine = s + "#" + rev_s; // 防止在匹配的时候,不从‘#’后开始
int sz = combine.size();
vector<int> match(sz, );
int k = ;
for (int i = ; i < sz; i++) {
while (k > && combine[i] != combine[k])
k = match[k - ];
if (combine[i] == combine[k])
k = k + ;
match[i] = k;
}
return rev_s.substr(, s.size() - match[sz - ]) + s;
}
};
上边程序可以解释如下:
对构造的字符串combine进行匹配操作后,得到如下结果:

匹配部分也就是s和rev_s重复的部分,而不匹配的部分就是它们不一样的部分。接下来的字符串拼接操作就是:

拼接完成后即是最终的结果。
LeetCode之“字符串”:最短回文子串的更多相关文章
- LeetCode:最长回文子串【5】
LeetCode:最长回文子串[5] 题目描述 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: ...
- 【LeetCode】最长回文子串【动态规划或中心扩展】
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad"输出: "bab"注意: " ...
- Java实现 LeetCode 5 最长回文子串
5. 最长回文子串 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab&quo ...
- leetcode python最长回文子串
回文的意思是正着念和倒着念一样,如:上海自来水来自海上,雾锁山头山锁雾,天连水尾水连天 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: & ...
- [LeetCode] 5. 最长回文子串
题目链接:https://leetcode-cn.com/problems/longest-palindromic-substring/ 题目描述: 给定一个字符串 s,找到 s 中最长的回文子串.你 ...
- LeetCode 05 最长回文子串
题目 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" 注意: ...
- [LeetCode] 5. 最长回文子串 ☆☆☆(最长子串、动态规划)
最长回文子串 (动态规划法.中心扩展算法) https://leetcode-cn.com/problems/longest-palindromic-substring/solution/xiang- ...
- 【Leetcode】最长回文子串
启发 1)做题前一定要读懂题目 在本题中,首先要清楚地定义回文子串的概念,然后才能设计算法查找它. 如中心扩散法,其主要思想在于找到一个回文子串的定义——两侧互为镜像.进一步分为奇数长度和偶数长度进行 ...
- 【LeetCode】最长回文子串-动态规划法
[问题]给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 : 输入: "babad" 输出: "bab" 注意: ...
- 【LeetCode】最长回文子串-中心扩展法
[问题]给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 : 输入: "babad" 输出: "bab" 注意: ...
随机推荐
- RxJava操作符(05-结合操作)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51656736 本文出自:[openXu的博客] 目录: CombineLatest Join ...
- 阻尼回弹效果的ScrollView嵌套GridView
以前写过一篇带阻尼回弹效果的ScrollView,但是有些小问题,于是又重新整理了一下,这篇文章一是一个带阻尼的Scrollview,再个就是Scrollview嵌套GridView实现,而GridV ...
- JDBC的java驱动安装
首先登陆mysql.com官方网站,download-->选中下面的community–>mysql connentor-->然后选中下面与平台无关的zip包,一般是第二个,完成下载 ...
- CentOS一般用户和root用户之间的切换
如果终端提示符显示为"$",表明该用户为普通用户.输入su,回车,然后输入root密码,即可切换到root用户.如果是root用户想切换回普通用户,输入"su 用户名&q ...
- 【Android应用开发】Android Studio 错误集锦 -- 将所有的 AS 错误集合到本文
. 一. 编译错误 1. "AndroidManifest.xml file not found" 错误 (1) 报错信息 报错信息 : -- Message Make : Inf ...
- Ruby开发入门
开发环境搭建 首先安装Ruby SDK,我安装的版本是2.0.之后安装IDE,这里用的是Jetbrain的RubyMine 5.4.3,注意是否支持对应版本的Ruby SDK. 一段神奇的注册码... ...
- [Err] 1093 - You can't specify target table 's' for update in FROM clause
[Err] 1093 - You can't specify target table 's' for update in FROM clause 执行SQL DELETE from book WHE ...
- CUDA程序的调试总结【不定时更新】
1 )CUDA的程序,经常犯,但是很难发现的一个错误就是同步问题. 描述下实例 for (k = 0; k < N; k+=BS) { sda[tx] = gda[tx+index]; __sy ...
- hibernate 关于主键
本文为北京尚学堂hibernate视频的学习笔记 1在xml中定义单个主键生成策略 1.1通过xml <id name="id" type="long" ...
- cocos2d-x初探
今天把cocos2d-x下载下来装了准备试试. 就不用windows本了,主要想做iOS游戏,所以这里用mac. 先在http://cocos2d-x.org/download下载右边的cocos,然 ...