-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode5.py
More file actions
35 lines (28 loc) · 736 Bytes
/
code5.py
File metadata and controls
35 lines (28 loc) · 736 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"2"
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
re = [0, ]
te = []
def hui(ls, rs):
n = rs - ls + 1
if n > re[-1]:
re.append(rs - ls + 1)
te.append((ls, rs))
if ls == 0 or rs == len(s) - 1:
return
if s[ls - 1] == s[rs + 1]:
hui(ls - 1, rs + 1)
return
lz = 0
while lz < (len(s) - 1):
hui(lz, lz)
if s[lz + 1] == s[lz]:
hui(lz, lz + 1)
lz = lz + 1
if len(s) == 1:
return s
return s[te[-1][0]:te[-1][1] + 1]