Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitca3a493

Browse files
committed
feat: add 005
1 parent12d9074 commitca3a493

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
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|

‎note/005/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
classSolution {
35+
int st, end;
36+
37+
publicStringlongestPalindrome(Strings) {
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+
privatevoidhelper(char[]chars,intl,intr) {
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
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
packagecom.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+
publicclassSolution {
12+
intst,end;
13+
14+
publicStringlongestPalindrome(Strings) {
15+
intlen =s.length();
16+
if (len <=1)returns;
17+
char[]chars =s.toCharArray();
18+
for (inti =0;i <len;i++) {
19+
helper(chars,i,i);
20+
helper(chars,i,i +1);
21+
}
22+
returns.substring(st,end +1);
23+
}
24+
25+
privatevoidhelper(char[]chars,intl,intr) {
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+
publicstaticvoidmain(String[]args) {
37+
Solutionsolution =newSolution();
38+
System.out.println(solution.longestPalindrome("babad"));
39+
System.out.println(solution.longestPalindrome("cbbd"));
40+
}
41+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp