File tree Expand file tree Collapse file tree 3 files changed +119
-0
lines changed 
src/com/blankj/medium/_005 Expand file tree Collapse file tree 3 files changed +119
-0
lines changed Original file line number Diff line number Diff line change 6969| :------------- | :------------- | :------------- | 
7070| 2| [ Add Two Numbers] [ 002 ] | Linked List, Math| 
7171| 3| [ Longest Substring Without Repeating Characters] [ 003 ] | Hash Table, Two Pointers, String| 
72+ | 5| [ Longest Palindromic Substring] [ 005 ] | String| 
7273| 8| [ String to Integer (atoi)] [ 008 ] | Math, String| 
7374| 15| [ 3Sum] [ 015 ] | Array, Two Pointers| 
7475| 17| [ Letter Combinations of a Phone Number] [ 017 ] | String, Backtracking| 
Original file line number Diff line number Diff line change 1+ # [ Longest Palindromic Substring] [ title ]  
2+ 
3+ ## Description  
4+ 
5+ Given a string ** s** , find the longest palindromic substring in ** s** . You may assume that the maximum length of ** s**  is 1000.
6+ 
7+ ** Example:** 
8+ 
9+ ``` 
10+ Input: "babad" 
11+ 
12+ Output: "bab" 
13+ 
14+ Note: "aba" is also a valid answer. 
15+ 
16+ ``` 
17+ 
18+ ** Example:** 
19+ 
20+ ``` 
21+ Input: "cbbd" 
22+ 
23+ Output: "bb" 
24+ ``` 
25+ 
26+ ** Tags:**  String
27+ 
28+ 
29+ ## 思路0  
30+ 
31+ 题意是寻找出字符串中最长的回文串,所谓回文串就是正序和逆序相同的字符串,也就是关于中间对称。我们先用最常规的做法,依次去求得每个字符的最长回文,要注意每个字符有奇数长度的回文串和偶数长度的回文串两种情况,相信你可以很轻易地从如下代码中找到相关代码,记录最长回文的始末位置即可,时间复杂度的话,首先要遍历一遍字符串,然后对每个字符都去求得最长回文,所以时间复杂度为O(n^2)。
32+ 
33+ ``` java 
34+ class  Solution  {
35+  int  st, end;
36+ 
37+  public  String  longestPalindrome (String  s ) {
38+  int  len =  s. length();
39+  if  (len <=  1 ) return  s;
40+  char [] chars =  s. toCharArray();
41+  for  (int  i =  0 ; i <  len; i++ ) {
42+  helper(chars, i, i);
43+  helper(chars, i, i +  1 );
44+  }
45+  return  s. substring(st, end +  1 );
46+  }
47+ 
48+  private  void  helper (char [] chars , int  l , int  r ) {
49+  while  (l >=  0  &&  r <  chars. length &&  chars[l] ==  chars[r]) {
50+  -- l;
51+  ++ r;
52+  }
53+  if  (end -  st <  r -  l -  2 ) {
54+  st =  l +  1 ;
55+  end =  r -  1 ;
56+  }
57+  }
58+ }
59+ ``` 
60+ 
61+ ## 思路1  
62+ 
63+ 
64+ 
65+ ``` java 
66+ 
67+ ``` 
68+ 
69+ 
70+ ## 结语  
71+ 
72+ 如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[ awesome-java-leetcode] [ ajl ] 
73+ 
74+ 
75+ 
76+ [ title ] : https://leetcode.com/problems/longest-palindromic-substring 
77+ [ ajl ] : https://github.com/Blankj/awesome-java-leetcode 
Original file line number Diff line number Diff line change 1+ package  com .blankj .medium ._005 ;
2+ 
3+ /** 
4+  * <pre> 
5+  * author: Blankj 
6+  * blog : http://blankj.com 
7+  * time : 2017/11/04 
8+  * desc : 
9+  * </pre> 
10+  */ 
11+ public  class  Solution  {
12+  int  st , end ;
13+ 
14+  public  String  longestPalindrome (String  s ) {
15+  int  len  = s .length ();
16+  if  (len  <= 1 ) return  s ;
17+  char [] chars  = s .toCharArray ();
18+  for  (int  i  = 0 ; i  < len ; i ++) {
19+  helper (chars , i , i );
20+  helper (chars , i , i  + 1 );
21+  }
22+  return  s .substring (st , end  + 1 );
23+  }
24+ 
25+  private  void  helper (char [] chars , int  l , int  r ) {
26+  while  (l  >= 0  && r  < chars .length  && chars [l ] == chars [r ]) {
27+  --l ;
28+  ++r ;
29+  }
30+  if  (end  - st  < r  - l  - 2 ) {
31+  st  = l  + 1 ;
32+  end  = r  - 1 ;
33+  }
34+  }
35+ 
36+  public  static  void  main (String [] args ) {
37+  Solution  solution  = new  Solution ();
38+  System .out .println (solution .longestPalindrome ("babad" ));
39+  System .out .println (solution .longestPalindrome ("cbbd" ));
40+  }
41+ }
                                 You can’t perform that action at this time. 
               
                  
0 commit comments