LeetCode——Longest Palindromic Subsequence
1. Question
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".
2. Solution
动态规划。依次求出所有长度的子字符串的最长回文子序列。
dp[i][i + j] = max(s[i] == s[i + j] ? dp[i + 1][i + j - 1] + 2 : dp[i + 1][i + j - 1], max(dp[i + 1][i + j], dp[i][i + j - 1])); 其中i表示起点,j表示子字符串长度。
3. Code
class Solution {
public:
int longestPalindromeSubseq(string s) {
// dp
int len = s.length();
vector<vector<int>> dp(len, vector<int>(len, 1));
for (int j = 1; j < s.length(); j++) {
for (int i = 0; i < s.length() - j; i++) {
dp[i][i + j] = max(dp[i + 1][i + j], dp[i][i + j - 1]);
if (s[i] == s[i + j]) {
if (i + 1 <= i + j - 1) {
dp[i][i + j] = max(dp[i][i + j], dp[i + 1][i + j - 1] + 2);
} else
dp[i][i + j] = max(dp[i][i + j], j + 1);
} else {
if (i + 1 <= i + j - 1)
dp[i][i + j] = max(dp[i][i + j], dp[i + 1][i + j - 1]);
}
}
}
return dp[0][s.length() - 1];
}
};
LeetCode——Longest Palindromic Subsequence的更多相关文章
- [LeetCode] Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- [Leetcode] Longest Palindromic Subsequence
Longest Palindromic Subsequence 题解 题目来源:https://leetcode.com/problems/longest-palindromic-subsequenc ...
- [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...
- LN : leetcode 516 Longest Palindromic Subsequence
lc 516 Longest Palindromic Subsequence 516 Longest Palindromic Subsequence Given a string s, find th ...
- [leetcode]516. Longest Palindromic Subsequence最大回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [Swift]LeetCode516. 最长回文子序列 | Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- 516. Longest Palindromic Subsequence最长的不连续回文串的长度
[抄题]: Given a string s, find the longest palindromic subsequence's length in s. You may assume that ...
随机推荐
- Spark Standalone Mode 多机启动 -- 分布式计算系统spark学习(二)(更新一键启动slavers)
捣鼓了一下,先来个手动挡吧.自动挡要设置ssh无密码登陆啥的,后面开搞. 一.手动多台机链接master 手动链接master其实上篇已经用过. 这里有两台机器: 10.60.215.41 启动mas ...
- Django内置form表单和ajax制作注册页面
settings.py import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_D ...
- uchome client.php
uchome 主要使用了php的call_user_func()函数,在uc_clinet/client.php中,一般指向uc_api_mysql,而 uc_api_mysql()函数 则负责分发到 ...
- JavaScript Big-Int
这个库是为JavaScript中的大整数操作,如加,减,乘,除,mod,比较等. 这个库的原理是模拟笔和纸的操作,你可以操作整数,大到你的RAM允许. 例 var bigInt = require(' ...
- TensorFlow学习笔记(五)图像数据处理
目录: 一.TFRecord输入数据格式 1.1 TFrecord格式介绍 1.2 TFRecord样例程序 二.图像数据处理 2.1TensorFlow图像处理函数 2.2图像预处理完整样例 三.多 ...
- Oracle DB 移动数据
描述移动数据的方式 • 创建和使用目录对象 • 使用SQL*Loader 加载非Oracle DB(或用户文件)中的数据 • 使用外部表并通过与平台无关的文件移动数据 • 说明Oracle 数据泵的 ...
- 使用jQuery创建节点、将节点插入到指定的位置
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 安装mysql8.0.11以及修改root密码、连接navicat for mysql。
最近在学习node.js,少不得要跟数据库打交道,于是打算安装一个数据库软件,在mongedb和mysql之间选择了mysql.作为一个数据库新人不敢评论孰好孰坏,最后选择mysql纯属因为公司在用m ...
- 杭电1019Least Common Multiple
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1019 题目: Problem Description The least common multiple ...
- ISAP模板
#include<bits/stdc++.h> using namespace std; using namespace std; typedef long long ll; const ...