Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.
Example 1: Input:
"bbbab"Output:
4One possible longest palindromic subsequence is "bbbb".
Example 2: Input:
"cbbd"Output:
2One possible longest palindromic subsequence is "bb".
时间复杂度 和空间复杂度 o(n^2)
通过观察可知,不管当si 和sj是不是相等时,他当前dp的值最多用到了len比当前len小2的dp的值,那么可以用三个滚动数组取代dp的二维数组 从而把空间复杂度降到o(n)
Last updated