516. Longest Palindromic Subsequence

  • Total Accepted: 10598
  • Total Submissions: 24947
  • Difficulty: Medium
  • Contributors:Stomach_ache

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:
4
One possible longest palindromic subsequence is "bbbb".

Example 2:
Input:

"cbbd"
Output:
2

One possible longest palindromic subsequence is "bb".


题解:运用递归的思想,当一个子序列长度为1时,最长回文长度为1,当长度为2时,若两位相等,则最长长度为2,否则为1。

当向一个已知最长回文长度的子序列的两边各加一位时,若两位相等,则最长长度是“只加左边”、“只加右边”、“原长度+2”三种情况的最大值。

若两位不相等,则是“只加左边”、“只加右边”、“原长度”三种情况的最大值。代码如下:

算法设计与应用基础:第十六周


相关文章:

  • 2021-10-26
  • 2021-07-17
  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
  • 2021-04-13
  • 2021-12-10
  • 2022-03-08
猜你喜欢
  • 2021-11-24
  • 2021-05-05
  • 2021-11-15
  • 2021-05-27
  • 2022-01-10
  • 2021-05-25
  • 2021-05-21
相关资源
相似解决方案