5. Longest Palindromic Substring

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

Difficulty: Medium

在字符串中找出回文子字符串,主要需要考虑的问题是

1.回文格式abba,abcba

2.容易出错的情况,aaaa,aaaaa

3.回文计数,abcba>abba

4.采用从中间往两边扩展的方法来计算

Submission 1  RT:568ms

 1 public string LongestPalindrome(string s) {
 2         if (string.IsNullOrEmpty(s)) return string.Empty;
 3             List<char> list = s.ToCharArray().ToList();
 4             int length = list.Count;
 5             int max = 0;
 6             List<char> longest = new List<char>();
 7 
 8             if (length == 1)
 9             {
10                 longest = new List<char>() {list[0]};
11             }
12 
13             for (int i = 0; i < length; i++)
14             {
15                 if ((i + 1 < length && list[i] == list[i + 1]))
16                 {
17                     int left, right, sum;
18                     List<char> l = new List<char>();
19 
20                     left = i;
21                     right = i + 1;
22                     sum = 1;
23                     l.Add(list[i]);
24                     l.Add(list[i + 1]);
25 
26                     while (left - 1 >= 0 && right + 1 < length)
27                     {
28                         if (list[left - 1] == list[right + 1])
29                         {
30                             left = left - 1;
31                             right = right + 1;
32                             sum++;
33                             l.Insert(0, list[left]);
34                             l.Add(list[right]);
35                         }
36                         else
37                         {
38                             break;
39                         }
40                     }
41                     if (sum >= max)
42                     {
43                         max = sum;
44                         longest = l;
45                     }
46                 }
47 
48                 if (i > 0 && i + 1 < length && list[i - 1] == list[i + 1])
49                 {
50                     int left, right, sum;
51                     List<char> l = new List<char>();
52                     left = i - 1;
53                     right = i + 1;
54                     sum = 2;
55                     l.Add(list[i - 1]);
56                     l.Add(list[i]);
57                     l.Add(list[i + 1]);
58                     while (left - 1 >= 0 && right + 1 < length)
59                     {
60                         if (list[left - 1] == list[right + 1])
61                         {
62                             left = left - 1;
63                             right = right + 1;
64                             sum++;
65                             l.Insert(0, list[left]);
66                             l.Add(list[right]);
67                         }
68                         else
69                         {
70                             break;
71                         }
72                     }
73                     if (sum > max)
74                     {
75                         max = sum;
76                         longest = l;
77                     }
78                 }
79             }
80             return new string(longest.ToArray());
81     }
View Code

精简版Submission 2 RT:120ms

 1 public string LongestPalindrome(string s)
 2         {
 3             if (string.IsNullOrEmpty(s)) return string.Empty;
 4 
 5             int start = 0, end = 0;
 6             for (int i = 0; i < s.Length; i++)
 7             {
 8                 int len1 = ExpandAroundCenter(s, i, i);
 9                 int len2 = ExpandAroundCenter(s, i, i+1);
10                 int len = Math.Max(len1, len2);
11                 if (len > end - start)
12                 {
13                     start = i - (len - 1)/2;
14                     end = i + len/2;
15                 }
16             }
17             return s.Substring(start, end - start + 1);
18         }
19 
20         private int ExpandAroundCenter(string s, int left, int right)
21         {
22             int l = left, r = right;
23             while (l >= 0 && r < s.Length && s[l] == s[r])
24             {
25                 l--;
26                 r++;
27             }
28             return r - l - 1;
29         }
View Code

 

posted @ 2016-07-25 16:12  AshLeakey  阅读(124)  评论(0编辑  收藏  举报