214. Shortest Palindrome
一、题目
1、审题
2、分析
给出一个字符串,在此字符串前边添加字符,使得其成为一个回文,求添加最少字符后,所形成的回文。
二、解答
1、思路:
方法一、
①、为了处理回文字符数为奇数和偶数的问题,先在字符串 s 的每一个字符之间插入字符 '#',并将每个字符放入一个 List 中
②、下标 index 依次从 1 到 mid,定义两个指针 left = index - 1、right = index + 1;
比较 left 、right 所指元素是否相等,若不相等,则 index 向后移动,若相等,则 left、right相反移动,直到 left < 0,此时,即为以 index 为中心,形成的字符串为回文。记录最大的回文中心的字符下标 index
③、将 ②所求得的最大 index 为中心的字符串所不包含的 s 的后部分子字符串逆序插入一个 StringBuilder 中,并将 s 插入 此 StringBuilder 中,返回。
1 public String shortestPalindrome(String s) { 2 3 List<Character> list = new ArrayList<>(); 4 list.add('#'); 5 for(char c: s.toCharArray()) { 6 list.add(c); 7 list.add('#'); 8 } 9 10 int len = list.size(); 11 int mid = (len - 1) / 2; 12 int i = 1; 13 int max = 0; 14 while(i <= mid) { 15 int left = i - 1; 16 int right = i + 1; 17 while(left >= 0 && right <= len - 1) { 18 if(list.get(left) != list.get(right)) 19 break; 20 left--; 21 right++; 22 } 23 if(left <= 0) 24 max = i; 25 i++; 26 } 27 max = max * 2 + 1; 28 StringBuilder sb = new StringBuilder(); 29 for (int j = len - 1; j >= max; j--) { 30 if(list.get(j) != '#') 31 sb.append(list.get(j)); 32 } 33 sb = sb.append(s); 34 return sb.toString(); 35 }
方法二、
采用 KMP 的 Next 数组。
①、构造新的字符串 tmp = s + '#' + s.reverse + ‘_’;
②、求出 tmp 的 KMP 算法的 Next 数组。其中 next 的最后一个元素对应的字符是 ‘_’ ,代表 ‘_’ 之前的字符串能匹配的最长前缀后缀长度 len。
'#' 的作用是使得 '_' 对应的 next 数组值 len 不超过 s 的长度。
③、所求的最短回文即为: s 下标 从 len 开始的后部分子串翻转 + s 。
以 aacecaaa 为例:
1 public String shortestPalindrome2(String s) { 2 3 String tmp = s + "#" + new StringBuffer(s).reverse().toString() + "_"; 4 int[] table = getTable(tmp); 5 6 return new StringBuffer(s.substring(table[table.length - 1])).reverse().toString() + s; 7 } 8 9 10 private int[] getTable(String s) { 11 int[] next = new int[s.length()]; 12 next[0] = -1; 13 int k = -1; 14 int j = 0; 15 while(j < s.length() - 1) { 16 if(k == -1 || s.charAt(k) == s.charAt(j)) { 17 ++k; 18 ++j; 19 next[j] = k; 20 } 21 else { 22 k = next[k]; 23 } 24 } 25 26 return next; 27 }
方法三、
采用递归
①、找出从下标 0 开始的可能最大回文 str1,则 s 剩下子串为 str2;
②、str3 是 str2 的翻转,继续递归 str1,直到可以确定 str1 为一个回文,则返回的结果为 str3 + str1 + str2;
public String shortestPalindrome3(String s) {int j = 0;for (int i = s.length() - 1; i >= 0; i--) {if(s.charAt(i) == s.charAt(j))j++;}if(j == s.length())return s;String suffix = s.substring(j);return new StringBuffer(suffix).reverse().toString()+ shortestPalindrome3(s.substring(0, j)) + suffix;}
方法四、
采用循环,判断 s 从下标 0 开始的构成的最大回文子串 str,最终将 s 中剩下的末尾的不包含在 str 中的子串进行翻转并 加上 s 返回即可。
1 public String shortestPalindrome4(String s) { 2 int i = 0, end = s.length() - 1, j = end; 3 char chs[] = s.toCharArray(); 4 while(i < j) { 5 if(chs[i] == chs[j]) { 6 i++; 7 j--; 8 } 9 else { 10 i = 0; 11 j = --end; 12 } 13 } 14 return new StringBuilder(s.substring(end + 1)).reverse().toString() + s; 15 }
转载于:https://www.cnblogs.com/skillking/p/9886420.html
214. Shortest Palindrome相关推荐
- 【To Understand! 回文串6 KMP算法】LeetCode 214. Shortest Palindrome
LeetCode 214. Shortest Palindrome Solution1:最笨的方法.时间复杂度O(n2)O(n2)O(n^2) 竟然能AC,xixixi class Solution ...
- leetcode 214 Shortest Palindrome
lc214 Shortest Palindrome 可以将问题转化成找到原字符串的最长palindrome子串(注意,子串必须以s[0]为起始) 例如:sdserf sds为最长palindrome子 ...
- leetcode 214. Shortest Palindrome | 214. 最短回文串(Java)
题目 https://leetcode.com/problems/shortest-palindrome/ 题解 看了 Related Topics - Rolling Hash 下的相关题目,看到了 ...
- LeetCode Shortest Palindrome(kmp的妙用)
题意:给出一个字符串,通过在字符串前添加字符,使得新的字符串为最短的对称字符串 思路:假设原字符串为s,将其取反的字符串为text,求text的后缀与s的前缀相同时的索引,将text与s索引后的字符拼 ...
- Leetcode重点250题
LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...
- LEETCODE-刷题个人笔记 Python(1-400)-TAG标签版本
1. Array (1) 27. Remove Element(Easy) 给定数组nums和值val,在适当位置删除该值的所有实例并返回新长度. 思路: 不需要使用排序,如果等于该值,则将n-1的值 ...
- LeetCode github集合,附CMU大神整理笔记
Github LeetCode集合 本人所有做过的题目都写在一个java项目中,同步到github中了,算是见证自己的进步.github目前同步的题目是2020-09-17日之后写的题.之前写过的题会 ...
- LeetCode刷题记录+数据结构总结
题号 思路 时间 8. String to Integer (atoi) 没想到有限自动机,写的太臃肿,边界条件考虑的也不足,用DFA分析起来就会很舒服 2020.4.3 11. Container ...
- 寒假LeetCode打卡
文章目录 @[toc] 链表专题 LeetCode 19. Remove Nth Node From End of List LeetCode 83. Remove Duplicates from S ...
- LEETCODE-刷题个人笔记 Python(1-400)
按tag分类,250/400的重点题目 LEETCODE-刷题个人笔记 Python(1-400)-TAG标签版本 1.Two Sum(easy) 给定一个整型数组,找出能相加起来等于一个特定目标数字 ...
最新文章
- 服务器技术综述(四)
- php if require,php echo()和print()、require()和include()函数区别说明
- idc网站html源码,40个网页常用小代码
- VC的文件路径为什么要用双斜杠
- 72000oracle,oracle 使用入门到放弃errorCode 1017, state 72000 java.sql.SQLException: ORA-01017:...
- 2017计算机基础知识ppt,2017计算机基础试卷.doc
- 计算机微信开发中期检查表,毕业论文(设计)-中期检查报告(范文)61页
- 虚拟机体验NAS私人云(第四篇):虚拟机安装群晖DSM7.01系统(附赠新版DS918+和DS3615xs启动映像)
- 用计算机怎么按e,在计算器上e的多少次方怎样按
- Cisco Packet Tracer路由器ip简单配置(网关)
- 计算机基础技能应用查询中心,《计算机基础及技能训练》大纲
- 从桌面运维转到网工后,我是怎样成为大厂高级网工的
- “马赛克”真能去除了?老司机狂喜!
- 记录关于监听HDMI插拔广播
- android系统profile文件路径,Android Profile Tools 入门
- python设置背景颜色为豆绿色_eclipse 设置豆沙绿保护色,保护眼睛
- 走进VOT--《High Performance Visual Tracking with Siamese Region Proposal Network》阅读翻译
- 把梳子卖给和尚的故事
- 腾讯短网址/短链接url.cn生成接口工具推荐
- Navicat Premium 15 完全卸载
热门文章
- Atitit 提升科技影响力----软件方面的.docx 目录 1. 大原则	2 1.1. 科技强人必须是创新型[	2 1.2. 要有一定的体量和规模	2 1.3. 产业链齐全 底层基础 --高层应
- Atitit 调用百度语音识别 目录 1. 建立一个音频app项目,获得appid kersec	1 2. 直接使用JAR包步骤如下:	1 2.1. public class baiduAudio
- Atitit 装备工具分类 attilax总结 艾龙著 工具链体系 武器与软件行业工具也是很近似的的。 1. 分类思维	1 1.1. 总分类:冷、热	1 1.2. 轻、重、大规模杀伤性	1
- paip.java 开发中web server的选择jboss resin tomcat比较..
- main.cpp first defined here 解决
- paip.discuz x2.5 用户及积分账户的接口attilax总结
- 阮一峰:WebSocket 教程
- (转)“跑批”发展编年史
- linux ubuntu 安装 matlab 2010 及破解 详细图解
- ECS 云助手,实现云上运维自动化