Shortest Palindrome
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"
.
分析:
就是找出从原字符串中从第一个字符开始能够组成的最长palindome. 然后把原字符串中后面部分reverse后放在前面,这样整个字符串就是palindrome, 并且长度最小。
public class Solution {
public String shortestPalindrome(String s) {
if (s == null || s.length() <= )
return s;
int size = ;
for (int i = ; i <= (s.length() - ) / ; i++) {
size = Math.max(size, maxSize(s, i, i));
size = Math.max(size, maxSize(s, i, i + ));
}
String str = s.substring(size);
StringBuilder sb = new StringBuilder(str);
sb.reverse();
return sb.toString() + s;
} private int maxSize(String s, int left, int right) {
while (left >= && right < s.length() && s.charAt(left) == s.charAt(right)) {
left--;
right++;
}
if (left == -) {
return right - left - ;
}
return ;
}
}
Shortest Palindrome的更多相关文章
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- LeetCode 214 Shortest Palindrome
214-Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- 【leetcode】Shortest Palindrome(hard)★
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- LeetCode Shortest Palindrome
原题链接在这里:https://leetcode.com/problems/shortest-palindrome/ 题目: Given a string S, you are allowed to ...
- 214. Shortest Palindrome
题目: Given a string S, you are allowed to convert it to a palindrome by adding characters in front of ...
- 【Leetcode】Shortest Palindrome
Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...
- 【回文】leetcode - Shortest Palindrome
题目: Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...
- [Swift]LeetCode214. 最短回文串 | Shortest Palindrome
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...
随机推荐
- 单元测试写cookie
我们在开发WEB项目的时候,一般应用逻辑跟ASPX页面是分离的项目.应用逻辑一般会是一个DLL组件项目.如果这个组件项目中A方法使用了Session.Cookie等信息的读写,则这个方法就很难写单元测 ...
- MongoVUE
MongoVUE运行界面如下:
- Effective Objective-C 2.0 — 第二条:类的头文件中尽量少引入其他头文件
第二条:类的头文件中尽量少引入其他头文件 使用向前声明(forward declaring) @class EOCEmployer 1, 将引入头文件的实际尽量延后,只在确有需要时才引入,这样就可以减 ...
- Web启动服务器上的某一个服务
情景是这样的.. 网页打开一个数据列表..数据要求实时从其他多个平台上获取.. 所以就有了一个Web页面..还有个WinService的服务来定时获取这些数据... 问题来了.. 发现Service有 ...
- 360chrome,google chrome浏览器使用jquery.ajax加载本地html文件
使用360chrome和google chrome浏览器加载本地html文件时,会报错,提示: XMLHttpRequest cannot load file:///Y:/jswg/code/html ...
- angularjs之自己定义指令篇
1>指令基础知识 directive() 参考资料 http://www.tuicool.com/articles/aAveEj http://damoqiongqiu.iteye.com/bl ...
- OB函数
ob_start 打开输出控制缓冲 ob_get_contents 返回输出缓冲区内容 ob_clean 清空( ...
- Python 文件遍历
Python具备强大的解析能力,其中列表解析甚至可以作用在某些并非实际存储的序列上,任何可遍历对象都可以,包括可自动逐步读取的文件. 例如下面的代码将会从逐行读取一个文本文件,并且在每一行的最后加上一 ...
- css定位之z-index问题分析
新手先去看看 CSS z-index 属性 CSS z-index 属性的使用方法和层级树的概念 ---------------------------------------------- ...
- Jquery控制滚动显示欢迎字幕v2
Jquery控制滚动显示欢迎字幕v2: 之前做的那个比较适合测试环境,但要套入到网站中,有两个按钮在那摆着,还是不太好看.后面对代码进行了修改,如下: 参考代码: <html> <h ...