码迷,mamicode.com
首页 > 编程语言 > 详细

leetcode 算法整理

时间:2017-12-28 19:45:13      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:insert   hid   div   play   turn   example   最大的   body   sys   

一 字符串中的最大回文串(第5题)

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of sis 1000.

Example:

Input: "babad"

Output: "bab"

Note: "aba" is also a valid answer.

 

Example:

Input: "cbbd"

Output: "bb"

 

1. 我的解法(accepted): 中心扩展

思路: 回文即代表有中心,一次遍历中,对于每个位置上的数字求一下最大的回文串即可,初始处理剪枝,合并掉同样的元素,如xxxxaaaxxxx,先常量级别把a合并掉; 遍历时候再次剪枝,遍历过的有一个maxLen,如果即将遍历的元素最大可能回文长度都不可能超过maxLen,不再遍历。

技术分享图片
 1     public static void main(String[] args) {
 2         String str = "ababababa";
 3         System.out.println(longestPalindrome(str));
 4 
 5     }
 6 
 7     public static String longestPalindrome(String s) {
 8         if (s == null) {
 9             return null;
10         }
11         char[] chars = s.toCharArray();
12         int length = chars.length;
13         int maxLen = 0;
14         String maxStr = "";
15 
16         for (int i = 0; i < length; i++) {
17 
18             // cut branch
19             int possibleLength = getMaxPossibleLength(i, length);
20             if (possibleLength < maxLen) {
21                 continue;
22             }
23 
24             String maxStrTmp = getMaxStrByIndex(i, chars);
25             if (maxLen < maxStrTmp.length()) {
26                 maxLen = maxStrTmp.length();
27                 maxStr = maxStrTmp;
28             }
29         }
30         return maxStr;
31     }
32 
33     private static int getMaxPossibleLength(int index, int length) {
34         int head = 0;
35         int tail = length - 1;
36         if (index == head || index == tail) {
37             return 1;
38         }
39         int result1 = index - head;
40         int result2 = tail - index;
41 
42         int min = result1 <= result2 ? result1 : result2;
43         return min * 2 + 1;
44     }
45 
46     private static String getMaxStrByIndex(int index, char[] chars) {
47         StringBuilder sb = new StringBuilder(String.valueOf(chars[index]));
48         int length = chars.length;
49         int head = index - 1;
50         int tail = index + 1;
51 
52         // middle deal
53         while (true) {
54             if (head >= 0 && chars[index] == chars[head]) {
55                 sb.insert(0, String.valueOf(chars[head--]));
56             } else if (tail <= length - 1 && chars[index] == chars[tail]) {
57                 sb.append(String.valueOf(chars[tail++]));
58             } else {
59                 break;
60             }
61         }
62 
63         // besides deal
64         while (true) {
65             if (head < 0 || tail > length - 1) {
66                 break;
67             }
68             if (head >= 0 && tail <= length - 1 && chars[head] == chars[tail]) {
69                 sb.insert(0, String.valueOf(chars[head--]));
70                 sb.append(String.valueOf(chars[tail++]));
71                 continue;
72             }
73             break;
74 
75         }
76         return sb.toString();
77     }
View Code

 

2. dp解法

 

 



leetcode 算法整理

标签:insert   hid   div   play   turn   example   最大的   body   sys   

原文地址:https://www.cnblogs.com/balfish/p/8137229.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!