题目链接

  题目要求: 

  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. Spring之WEB模块

    Spring的WEB模块用于整合Web框架,例如Struts 1.Struts 2.JSF等 整合Struts 1 继承方式 Spring框架提供了ActionSupport类支持Struts 1的A ...

  2. 如何正确使用const、static、extern

    转自:http://www.jianshu.com/p/2fd58ed2cf55 前言 本篇文章主要介绍在开发中怎么使用const.static.extern关键字. 一.const 与宏的区别: c ...

  3. 设置 NSZombieEnabled 定位 EXC_BAD_ACCESS 错误

    我们做 iOS 程序开发时经常用遇到 EXC_BAD_ACCESS 错误导致 Crash,出现这种错误时一般 Xcode 不会给我们太多的信息来定位错误来源,只是在应用 Delegate 上留下像 T ...

  4. SpriteKit关于SKScene中的渲染Loop

    在本节中,我将来说明一下SKScene在SKView显示之后发生了神马. 在更传统的iOS app中,你可能只会渲染view的内容仅仅一次,然后它将保持静态直到view的模式发生了显示的改变,这对于商 ...

  5. [nginx]查看安装了哪些模块

    有时候安装的时候不知道哪些模块忘了安装需要查看下已经安装的模块. 查看安装了哪些模块 $ nginx -V nginx version: nginx/1.4.6 (Ubuntu) built by g ...

  6. 1.httpClient和ScrollView

    1 在服务器端使用sqllite编写数据库 常见命令是:sqlite3 tank.db 进入之后创建表: create table tscore ( id integer primary key au ...

  7. 自制Linux 终端 锁屏防窃助手

    很多时候我们不能一直守护在自己的电脑旁边,而且有些文件并不想让别人知道.那么这时候来个锁屏,是再合适不过的了.今天分享一个自制的锁屏工具,如下. 准备 操作系统 : 我这里是ElementaryOS虚 ...

  8. android最新更新方法

    使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository-6.xm ...

  9. iOS中 语音识别功能/语音转文字教程详解 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 原文地址:http://blog.csdn.net/qq_31810357/article/details/5111 ...

  10. 安卓中不同APP之间的消息通信

    昨天在腾讯实习生招聘初试面试时面试官问道我关于两个APP之间相互通信的方式,当时自己回道到了contentProvider与BroadcastReceiver.但他接着问还有没有其它的方式,我跟他说可 ...