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

Commit aedd7dc

Browse files
add cleaner solution
1 parent ff9bf7a commit aedd7dc

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

‎Kangli/DP/longestPalindromicSubstring.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
#clean dp solution
2+
class Solution(object):
3+
def longestPalindrome(self, s):
4+
"""
5+
:type s: str
6+
:rtype: str
7+
"""
8+
max_len, res = 0, ""
9+
dp = [[False for x in range(len(s))] for y in range(len(s))]
10+
for i in range(len(s)-1, -1, -1):
11+
for j in range(i, len(s)):
12+
dp[i][j] = s[i] == s[j] and (j-i < 3 or dp[i+1][j-1])
13+
if dp[i][j] and j-i+1 > len(res):
14+
res = s[i:j+1]
15+
return res
16+
117
class Solution(object):
218
def longestPalindrome(self, s):
319
st = 0

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /