Skip to content

Commit bd665fe

Browse files
Add files via upload
1 parent d4b58bb commit bd665fe

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 1004ms 77.20%
2+
# 基本思路是,对于每个字符和两个字符的中间,都可能成为一个回文子字符串的中心
3+
# 因此,使用线性的扫描,对每个中心进行查找
4+
class Solution:
5+
def longestPalindrome(self, s):
6+
"""
7+
:type s: str
8+
:rtype: str
9+
"""
10+
if not s:
11+
return ''
12+
len_str, max_length, index = len(s), 0, 0.5
13+
while index < len_str - 1:
14+
left = int(index - 0.5)
15+
right = int(index) + 1
16+
length = 1 if int(index) == index else 0
17+
18+
while left >= 0 and right <= len_str - 1:
19+
if s[left] == s[right]:
20+
length += 2
21+
if length > max_length:
22+
max_length = length
23+
max_sub_string = s[left:right + 1]
24+
left -= 1;right += 1
25+
else:
26+
break
27+
index += 0.5
28+
return s[0] if max_length is 0 else max_sub_string

0 commit comments

Comments
 (0)