题目链接

  题目要求: 

  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之“字符串”:最短回文子串的更多相关文章

  1. LeetCode:最长回文子串【5】

    LeetCode:最长回文子串[5] 题目描述 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: ...

  2. 【LeetCode】最长回文子串【动态规划或中心扩展】

    给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad"输出: "bab"注意: " ...

  3. Java实现 LeetCode 5 最长回文子串

    5. 最长回文子串 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab&quo ...

  4. leetcode python最长回文子串

    回文的意思是正着念和倒着念一样,如:上海自来水来自海上,雾锁山头山锁雾,天连水尾水连天 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: & ...

  5. [LeetCode] 5. 最长回文子串

    题目链接:https://leetcode-cn.com/problems/longest-palindromic-substring/ 题目描述: 给定一个字符串 s,找到 s 中最长的回文子串.你 ...

  6. LeetCode 05 最长回文子串

    题目 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" 注意: ...

  7. [LeetCode] 5. 最长回文子串 ☆☆☆(最长子串、动态规划)

    最长回文子串 (动态规划法.中心扩展算法) https://leetcode-cn.com/problems/longest-palindromic-substring/solution/xiang- ...

  8. 【Leetcode】最长回文子串

    启发 1)做题前一定要读懂题目 在本题中,首先要清楚地定义回文子串的概念,然后才能设计算法查找它. 如中心扩散法,其主要思想在于找到一个回文子串的定义——两侧互为镜像.进一步分为奇数长度和偶数长度进行 ...

  9. 【LeetCode】最长回文子串-动态规划法

    [问题]给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 : 输入: "babad" 输出: "bab" 注意: ...

  10. 【LeetCode】最长回文子串-中心扩展法

    [问题]给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 : 输入: "babad" 输出: "bab" 注意: ...

随机推荐

  1. Python rich comparisons 自定义对象比较过程和返回值

    Classes wishing to support the rich comparison mechanisms must add one or more of the following new ...

  2. static修饰符详解

    static表示"全局"或者"静态"的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static ...

  3. 深入了解UIViewController控制器与对应的View类的详解

    ViewController是iOS开发中MVC模式中的C(视图控制器),ViewController是view的controller,ViewController的职责主要包括管理内部各个view的 ...

  4. iOS开发之使用block块进行数据遍历的方法

    看了一篇文章,发现遍历数组.字典中的数据时,除了使用for循环外,还可以使用block块进行操作,瞬间感觉iOS的语言代码确实有点高大上的感觉,下面就简单的介绍一下这个方法. 首先是最基本的运用形式, ...

  5. [shiro学习笔记]第三节 使用myeclipse导入apache shiro中的QuikStart example例子

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/40149131 shiro官网:http://shiro.apache.org/ shi ...

  6. javascript之类型转换

    JavaScript是一种无类型语言,但同时JavaScript提供了一种灵活的自动类型转换的处理方式.基本规则是,如果某个类型的值用于需要其他类型的值的环境中,JavaScript就自动将这个值转换 ...

  7. (NO.00004)iOS实现打砖块游戏(十三):伸缩自如,我是如意金箍棒(下)!

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 准备缩短反弹棒素材 和上一篇类似,我们如法炮制一张缩短后反弹棒的 ...

  8. android分包方案

    当一个app的功能越来越复杂,代码量越来越多,也许有一天便会突然遇到下列现象: 1. 生成的apk在2.3以前的机器无法安装,提示INSTALL_FAILED_DEXOPT 2. 方法数量过多,编译时 ...

  9. >/dev/null 2>&1

    >/dev/null 2>&1 大部分在 crontab 计划任务中都会年到未尾带 >/dev/null 2>&1,是什么意思呢? > 是重定向 /dev ...

  10. EBS DBA指南笔记(三)

    第五章 patching   patch的作用:解决应用代码的问题:安装新的特征:更新technology stack组件.打patch不是一个简单的过程,但我们也没必要深究里面每个细节. EBS的p ...