File tree Expand file tree Collapse file tree 3 files changed +89
-0
lines changed
solution/005.Longest Palindromic Substring Expand file tree Collapse file tree 3 files changed +89
-0
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
47
47
| # | Title | Tags |
48
48
| ---| ---| ---|
49
49
| 002 | [ Add Two Numbers] ( https://github.com/doocs/leetcode/tree/master/solution/002.Add%20Two%20Numbers ) | ` Linked List ` , ` Math ` |
50
+ | 005 | [ Longest Palindromic Substring] ( https://github.com/doocs/leetcode/tree/master/solution/005.Longest%20Palindromic%20Substring ) | ` String ` , ` Dynamic Programming ` |
50
51
| 015 | [ 3Sum] ( https://github.com/doocs/leetcode/tree/master/solution/015.3Sum ) | ` Array ` , ` Two Pointers ` |
51
52
| 019 | [ Remove Nth Node From End of List] ( https://github.com/doocs/leetcode/tree/master/solution/019.Remove%20Nth%20Node%20From%20End%20of%20List ) | ` Linked List ` , ` Two Pointers ` |
52
53
| 024 | [ Swap Nodes in Pairs] ( https://github.com/doocs/leetcode/tree/master/solution/024.Swap%20Nodes%20in%20Pairs ) | ` Linked List ` |
Original file line number Diff line number Diff line change
1
+ ## 最长回文子串
2
+ ### 题目描述
3
+
4
+ 给定一个字符串 ** s** ,找到 ** s** 中最长的回文子串。你可以假设 ** s** 的最大长度为1000。
5
+
6
+ ** 示例 1:**
7
+ ```
8
+ 输入: "babad"
9
+ 输出: "bab"
10
+ 注意: "aba"也是一个有效答案。
11
+ ```
12
+
13
+ ** 示例 2:**
14
+ ```
15
+ 输入: "cbbd"
16
+ 输出: "bb"
17
+ ```
18
+
19
+ ### 解法
20
+ - 解法1
21
+
22
+ 利用动态规划,二维数组 ` res ` 存储 ` [j, i] ` 区间是否为回文串。动态规划递推式:
23
+
24
+ ` res[j][i] = res[j + 1][i - 1] && chars[j] == chars[i] `
25
+
26
+ 此方法时间和空间复杂度均为 ` O(n²) ` 。
27
+
28
+ ``` java
29
+ class Solution {
30
+ public String longestPalindrome (String s ) {
31
+ if (s == null || s. length() < 1 ) {
32
+ return " " ;
33
+ }
34
+ String str = " " ;
35
+ char [] chars = s. toCharArray();
36
+ int len = chars. length;
37
+ boolean [][] res = new boolean [len][len];
38
+ int start = 0 ;
39
+ int max = 1 ;
40
+ for (int i = 0 ; i < len; ++ i) {
41
+ for (int j = 0 ; j <= i; ++ j) {
42
+
43
+ res[j][i] = i - j < 2
44
+ ? chars[j] == chars[i]
45
+ : res[j + 1 ][i - 1 ] && chars[j] == chars[i];
46
+
47
+ if (res[j][i] && max < i - j + 1 ) {
48
+ max = i - j + 1 ;
49
+ start = j;
50
+ }
51
+
52
+ }
53
+ }
54
+
55
+ return s. substring(start, start + max);
56
+
57
+ }
58
+ }
59
+ ```
60
+
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public String longestPalindrome (String s ) {
3
+ if (s == null || s .length () < 1 ) {
4
+ return "" ;
5
+ }
6
+ String str = "" ;
7
+ char [] chars = s .toCharArray ();
8
+ int len = chars .length ;
9
+ boolean [][] res = new boolean [len ][len ];
10
+ int start = 0 ;
11
+ int max = 1 ;
12
+ for (int i = 0 ; i < len ; ++i ) {
13
+ for (int j = 0 ; j <= i ; ++j ) {
14
+
15
+ res [j ][i ] = i - j < 2 ? chars [j ] == chars [i ] : res [j + 1 ][i - 1 ] && chars [j ] == chars [i ];
16
+
17
+ if (res [j ][i ] && max < i - j + 1 ) {
18
+ max = i - j + 1 ;
19
+ start = j ;
20
+ }
21
+
22
+ }
23
+ }
24
+
25
+ return s .substring (start , start + max );
26
+
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments