Skip to content

Commit 43cac70

Browse files
authored
Update 005._longest_palindromic_substring.md
1 parent c86e295 commit 43cac70

File tree

1 file changed

+100
-102
lines changed

1 file changed

+100
-102
lines changed

docs/Leetcode_Solutions/Python/005._longest_palindromic_substring.md

Lines changed: 100 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,28 @@ LCSuff(S1...p, T1...q) = LCS(S1...p1, T1...q-1) if S[p] = T[q] else 0
4545
https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Longest_common_substring#Python_2
4646

4747
这样也超时?
48-
```python
49-
class Solution(object):
50-
def longestPalindrome(self, s):
51-
"""
52-
:type s: str
53-
:rtype: str
54-
"""
55-
def lcs(s1, s2):
56-
m = [[0] * (1 + len(s2)) for i in xrange(1 + len(s1))]
57-
longest, x_longest = 0, 0
58-
for x in xrange(1, 1 + len(s1)):
59-
for y in xrange(1, 1 + len(s2)):
60-
if s1[x - 1] == s2[y - 1]:
61-
m[x][y] = m[x - 1][y - 1] + 1
62-
if m[x][y] > longest:
63-
longest = m[x][y]
64-
x_longest = x
65-
else:
66-
m[x][y] = 0
67-
return s1[x_longest - longest: x_longest]
68-
69-
return lcs(s, s[::-1])
48+
```
49+
class Solution(object):
50+
def longestPalindrome(self, s):
51+
"""
52+
:type s: str
53+
:rtype: str
54+
"""
55+
def lcs(s1, s2):
56+
m = [[0] * (1 + len(s2)) for i in xrange(1 + len(s1))]
57+
longest, x_longest = 0, 0
58+
for x in xrange(1, 1 + len(s1)):
59+
for y in xrange(1, 1 + len(s2)):
60+
if s1[x - 1] == s2[y - 1]:
61+
m[x][y] = m[x - 1][y - 1] + 1
62+
if m[x][y] > longest:
63+
longest = m[x][y]
64+
x_longest = x
65+
else:
66+
m[x][y] = 0
67+
return s1[x_longest - longest: x_longest]
68+
69+
return lcs(s, s[::-1])
7070
```
7171
因为以为这样s[::-1]已经很快了.
7272

@@ -98,92 +98,90 @@ we check if the substring’s indices are the same as the reversed substring’s
9898

9999
参考https://shenjie1993.gitbooks.io/leetcode-python/content/005%20Longest%20Palindromic%20Substring.html
100100
```python
101-
class Solution(object):
102-
def longestPalindrome(self, s):
103-
"""
104-
:type s: str
105-
:rtype: str
106-
"""
107-
n = len(s)
108-
109-
# empty or one char
110-
if n < 2:
111-
return s
112-
113-
# left index of the target substring
114-
l = 0
115-
# right index of the target substring
116-
r = 0
117-
# length of the longest palindromic substring for now
118-
m = 0
119-
# length of the current substring
120-
c = 0
121-
122-
# Whether the substring contains the first character or last character and is palindromic
101+
class Solution(object):
102+
def longestPalindrome(self, s):
103+
"""
104+
:type s: str
105+
:rtype: str
106+
"""
107+
n = len(s)
108+
109+
# empty or one char
110+
if n < 2:
111+
return s
112+
113+
# left index of the target substring
114+
l = 0
115+
# right index of the target substring
116+
r = 0
117+
# length of the longest palindromic substring for now
118+
m = 0
119+
# length of the current substring
120+
c = 0
121+
122+
# Whether the substring contains the first character or last character and is palindromic
123+
b = True
124+
for i in range(n):
125+
# Odd situation
126+
for j in range(min(n-i,i+1)):
127+
if s[i-j] != s [i+j]:
128+
b = False
129+
break
130+
else:
131+
c = 2 * j + 1
132+
133+
if c > m :
134+
l = i - j + 1 - b
135+
r = i + j + b
136+
m = c
123137
b = True
124-
for i in range(n):
125-
# Odd situation
126-
for j in range(min(n-i,i+1)):
127-
if s[i-j] != s [i+j]:
128-
b = False
129-
break
130-
else:
131-
c = 2 * j + 1
132-
133-
if c > m :
134-
l = i - j + 1 - b
135-
r = i + j + b
136-
m = c
137-
b = True
138-
139-
# Even situation
140-
for j in range(min(n - i - 1, i + 1)):
141-
if (s[i - j] != s[i + j + 1]):
142-
b = False
143-
break
144-
else:
145-
c = 2 * j + 2
146-
if (c > m):
147-
l = i - j + 1 - b
148-
r = i + j + 1 + b
149-
m = c
150-
b = True
151-
return s[l:r]
138+
139+
# Even situation
140+
for j in range(min(n - i - 1, i + 1)):
141+
if (s[i - j] != s[i + j + 1]):
142+
b = False
143+
break
144+
else:
145+
c = 2 * j + 2
146+
if (c > m):
147+
l = i - j + 1 - b
148+
r = i + j + 1 + b
149+
m = c
150+
b = True
151+
return s[l:r]
152152
```
153153
以上是参考版本,自己写的版本:
154154
```python
155-
class Solution(object):
156-
def longestPalindrome(self, s):
157-
"""
158-
:type s: str
159-
:rtype: str
160-
"""
161-
n = len(s)
162-
163-
m,l,r = 0,0,0
164-
165-
for i in range(n):
166-
# odd case
167-
for j in range(min(i+1,n-i)):
168-
if s[i-j] != s[i+j]:
155+
class Solution(object):
156+
def longestPalindrome(self, s):
157+
"""
158+
:type s: str
159+
:rtype: str
160+
"""
161+
n = len(s)
162+
163+
m,l,r = 0,0,0
164+
165+
for i in range(n):
166+
# odd case
167+
for j in range(min(i+1,n-i)):
168+
if s[i-j] != s[i+j]:
169+
break
170+
if 2*j + 1 > m :
171+
m = 2 * j + 1
172+
l = i-j
173+
r = i+j
174+
175+
176+
if i+1 < n and s[i] == s[i+1]:
177+
for j in range(min(i+1,n-i-1)):
178+
if s[i-j] != s[i+j+1]:
169179
break
170-
if 2*j + 1 > m :
171-
m = 2 * j + 1
180+
if 2 * j + 2 > m :
181+
m = 2*j +2
172182
l = i-j
173-
r = i+j
174-
175-
176-
if i+1 < n and s[i] == s[i+1]:
177-
for j in range(min(i+1,n-i-1)):
178-
if s[i-j] != s[i+j+1]:
179-
break
180-
if 2 * j + 2 > m :
181-
m = 2*j +2
182-
l = i-j
183-
r = i+j+1
184-
185-
186-
return s[l:r+1]
183+
r = i+j+1
184+
return s[l:r+1]
187185
```
188186

189187

0 commit comments

Comments
 (0)