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".

这是一道典型的dp题,用dp[][]来存储i,j两个pointer指向的string的一个char, 分别按照相等和不相等来处理即可。

 class Solution {
public int longestPalindromeSubseq(String s) {
int length = s.length();
int[][] dp = new int[length][length];
for (int i = ; i < length; i++) {
dp[i][i] = ;
}
for (int subLength = ; subLength <= length; subLength++) {
for (int i = ; i + subLength <= length; i++) {
int j = subLength + i - ;
if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = dp[i + ][j - ] + ;
} else {
dp[i][j] = Math.max(dp[i + ][j], dp[i][j - ]);
}
}
}
return dp[][length - ];
}
}

Longest Palindromic Subsequence的更多相关文章

  1. [LeetCode] Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  2. [Swift]LeetCode516. 最长回文子序列 | Longest Palindromic Subsequence

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  3. 516. Longest Palindromic Subsequence最长的不连续回文串的长度

    [抄题]: Given a string s, find the longest palindromic subsequence's length in s. You may assume that ...

  4. 516. Longest Palindromic Subsequence

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  5. [Leetcode] Longest Palindromic Subsequence

    Longest Palindromic Subsequence 题解 题目来源:https://leetcode.com/problems/longest-palindromic-subsequenc ...

  6. LeetCode——Longest Palindromic Subsequence

    1. Question Given a string s, find the longest palindromic subsequence's length in s. You may assume ...

  7. LN : leetcode 516 Longest Palindromic Subsequence

    lc 516 Longest Palindromic Subsequence 516 Longest Palindromic Subsequence Given a string s, find th ...

  8. [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  9. LC 516. Longest Palindromic Subsequence

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

随机推荐

  1. 4、路由事件 RoutedEvent

    路由事件的类型:具体参考https://www.cnblogs.com/jellochen/p/3475754.html Tunnel隧道方式:路由事件使用隧道策略,以便事件实例通过树向下路由(从根到 ...

  2. CDOJ 1132 酱神赏花 dp+单调栈降低复杂度+滚动数组

    酱神赏花 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 262143/262143KB (Java/Others) Submit St ...

  3. python测试网站访问速度

    # -*- coding: utf-8 -*- # @Author : Felix Wang # @time : 2018/8/13 22:13 # pip3 install pycurl impor ...

  4. java中jsp的EL的定义以及使用

    1.定义: EL(Expression Language) 是为了使JSP写起来更加简单.表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 JSP 中简化表达式的方 ...

  5. Mysql模拟故障恢复案例过程

    一.数据库全备,全备脚本如下: [root@leader script]# cat bak_all.sh #!/bin/bash#Date: 2019-12-08#Author: chan#Mail: ...

  6. LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)

    题目描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 由于 ...

  7. koa 项目实战(二)连接 mongodb 数据库

    1.配置文件 根目录/config/keys.js module.exports = { mongoURI: 'mongodb://127.0.0.1:27017/mongodb' } 2.启动文件 ...

  8. airflow当触发具有多层subDAG的任务的时候,出现[Duplicate entry ‘xxxx’ for key dag_id]的错误的问题处理

    当触发一个具有多层subDAG的任务时,会发现执行触发的task任务运行失败,但是需要触发的目标DAG已经在运行了,dag log 错误内容: [2019-11-21 17:47:56,825] {b ...

  9. hibernate映射配置

    1. 普通字段类型 2. 主键映射 单列主键映射 多列作为主键映射 主键生成策略,查看api:   5.1.2.2.1. Various additional generators 数据库: Q:一个 ...

  10. redis(2)事务的订阅与发布

    一.shell终端进行事务的订阅与发布(异步) 发布 : publish channel message [root@localhost ~]# redis-cli -p -h 192.168.42. ...