Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
longest-palindromic-subsequence.py 569 Bytes
Copy Edit Raw Blame History
Allen Liu authored 2018-10-12 01:33 +08:00 . remove sensitive question description
# Time: O(n^2)
# Space: O(n)
class Solution(object):
def longestPalindromeSubseq(self, s):
"""
:type s: str
:rtype: int
"""
if s == s[::-1]:
return len(s)
dp = [[1] * len(s) for _ in xrange(2)]
for i in reversed(xrange(len(s))):
for j in xrange(i+1, len(s)):
if s[i] == s[j]:
dp[i%2][j] = 2 + dp[(i+1)%2][j-1] if i+1 <= j-1 else 2
else:
dp[i%2][j] = max(dp[(i+1)%2][j], dp[i%2][j-1])
return dp[0][-1]
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search