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     }

原文地址:https://www.cnblogs.com/skillking/p/9886420.html

时间: 2024-12-30 03:05:39

214. Shortest Palindrome的相关文章

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. Find and return the shortest palindrome you can find by performing this transformation. For example: Given "aacecaaa", return "aaacecaaa&qu

[LeetCode-JAVA] 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 "aaacecaa

[LeetCode][JavaScript]Shortest Palindrome

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", retur

[leetcode]Shortest Palindrome

O(n^2)的方法,最后一个case超时.需要用kmp方法或者manacher方法才能O(n),先忽略了. class Solution: def isPalindrome(self, sub: str) -> bool: for i in range(len(sub) // 2): if sub[i] != sub[len(sub) - i - 1]: return False return True def shortestPalindrome(self, s: str) -> str:

继续过Hard题目.0209

http://www.cnblogs.com/charlesblc/p/6372971.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca

练练脑,继续过Hard题目

http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca

LeetCode All in One 题目汇总

228 Summary Ranges 21.10% Easy 227 Basic Calculator II 18.00% Medium 226 Invert Binary Tree 35.40% Easy 225 Implement Stack using Queues 29.60% Medium 224 Basic Calculator 15.80% Medium 223 Rectangle Area 25.60% Easy 222 Count Complete Tree Nodes 19.

继续过Hard题目

接上一篇:http://www.cnblogs.com/charlesblc/p/6283064.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 L

练几道,继续过Hard题目

http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca