Leetcode -- Shortest Palindrome
2015-10-28 22:05
288 查看
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
Given
分析:
只需要找到以第一个字符为起始点的最长回文子序列即可。
For example:
Given
"aacecaaa", return
"aaacecaaa".
Given
"abcd", return
"dcbabcd".
分析:
只需要找到以第一个字符为起始点的最长回文子序列即可。
class Solution { public: string shortestPalindrome(string s) { int n=s.size(); if(n==0) return s; int i; string t=s; reverse(t.begin(),t.end()); for(i=0;i<n;++i) if(s.substr(0,n-i)==t.substr(i)) break; return t.substr(0,i)+s; } };
相关文章推荐
- RHCS集群理论暨 最佳实践
- Scala并行
- 排序的特性
- 浅谈 malloc/free 和 new/delete
- 从左到右升序,从上到下升序的元素互异的二维数组元素查找问题
- [转]再谈移动端Web屏幕适配
- 151028
- OccTransVF
- C++ :stack介绍
- [c] ★ 宏和函数的区别
- TOJ 1547. To and Fro【栈和队列】
- AndroidMusicPlayer
- c++继承中的内存布局(转)
- WebForm网站和MVC网站运行机制的区别
- Java:网络编程之IP、URL
- SurfaceView的绘图机制
- Third scrum meeting - 2015/10/28
- 自定义圆形图片
- github使用
- 网络下的数据传输之XML数据的解析