leetcode 115不同的子序列

滚动数组:
/*****
下标从1开始
dp[i][j]:= numbers of subseq of S[1:j] equals T[1:i]
if(s[j]==t[i]):(那么之后的子串可以是是dp[i-1][j-1](match) 或dp[i][j-1] (not match))
dp[i][j]=dp[i-1][j-1]+
dp[i][j-1];
if(t[i]!=s[j]):
dp[i][j]=dp[i][j-1]; 初始化:
dp[0][*]=0 时间:O(n2) 空间O(n2)
使用滚动数组:
O(n)空间: *****/ class Solution {
public:
int numDistinct(string s, string t) {
int ls=s.length(),lt=t.length();
vector<long> dp(ls+,);
for(int i=;i<=lt;i++){
int pre=dp[],cur;
dp[]=;
for(int j=;j<=ls;j++){
cur=dp[j];
if(s[j-]==t[i-])
dp[j]=pre+dp[j-];
else
dp[j]=dp[j-];
pre=cur;
//cout<<dp[j]<<" ";
}
//cout<<endl;
}
return dp[ls];
}
};
/*****
下标从1开始
dp[i][j]:= numbers of subseq of S[1:j] equals T[1:i]
if(s[j]==t[i]):(那么之后的子串可以是是dp[i-1][j-1](match) 或dp[i][j-1] (not match))
dp[i][j]=dp[i-1][j-1]+
dp[i][j-1];
if(t[i]!=s[j]):
dp[i][j]=dp[i][j-1]; 初始化:
dp[0][*]=0 时间:O(n2) 空间O(n2)
使用滚动数组:
O(n)空间: *****/ class Solution {
public:
int numDistinct(string s, string t) {
int ls=s.length(),lt=t.length();
vector<vector<long>> dp(lt+,vector<long>(ls+));
fill(begin(dp[]),end(dp[]),);
for(int i=;i<=lt;i++){
for(int j=;j<=ls;j++){
if(s[j-]==t[i-])
dp[i][j]=dp[i-][j-]+dp[i][j-];
else
dp[i][j]=dp[i][j-];
//cout<<dp[i][j]<<" ";
}
//cout<<endl;
}
return dp[lt][ls];
}
};
leetcode 115不同的子序列的更多相关文章
- Java实现 LeetCode 115 不同的子序列
115. 不同的子序列 给定一个字符串 S 和一个字符串 T,计算在 S 的子序列中 T 出现的个数. 一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字 ...
- Leetcode 115.不同的子序列
不同的子序列 给定一个字符串 S 和一个字符串 T,计算在 S 的子序列中 T 出现的个数. 一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串.(例 ...
- LeetCode 115.不同的子序列 详解
题目详情 给定一个字符串 S 和一个字符串 T,计算在 S 的子序列中 T 出现的个数. 一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串.(例如, ...
- [LeetCode] 115. Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- [LeetCode 115] - 不同子序列(Distinct Subsequences)
问题 给出字符串S和T,计算S中为T的不同的子序列的个数. 一个字符串的子序列是一个由该原始字符串通过删除一些字母(也可以不删)但是不改变剩下字母的相对顺序产生的一个新字符串.如,ACE是ABCDE的 ...
- [leetcode]115. Distinct Subsequences 计算不同子序列个数
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- [LeetCode] Is Subsequence 是子序列
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Increasing Subsequences 递增子序列
Given an integer array, your task is to find all the different possible increasing subsequences of t ...
随机推荐
- Docker容器入门之一:部署SpringBoot项目
一.环境准备: 1.vm虚拟机: Workstation 12 Pro 12.5.7 build-5813279 2.Centos 7 在虚拟机上安装好Centos7系统后,就可以开始准备安装D ...
- Java LinkedHashMap学习
以前一直使用HashMap,今天学习一下LinkedHashMap JavaDoc 注解: Hash table and linked list implementation of the Map i ...
- 一般情况下的NB-IoT网络架构
一. NB总体网络架构 NB-IoT端到端系统架构如下图所示: 终端:UE(User Equipment),通过空口连接到基站(eNodeB(evolved Node B , E-UTRAN 基站)) ...
- CentOS7 配置NFS(Network File System)及其使用
1. 服务端配置 1.1. 安装NFS yum -y install nfs* 1.2. 查看是否安装了NFS与RPCBIND rpm -qa | grep nfs rpm - ...
- plsql执行sql
第一步找执行的命令:: plsql ::::::::::File----->>>>Change Windows to ------->>>Command Wi ...
- Lua语言基本语法~运算符
Lua 变量 变量在使用前,必须在代码中进行声明,即创建该变量. 编译程序执行代码之前编译器需要知道如何给语句变量开辟存储区,用于存储变量的值. Lua 变量有三种类型:全局变量.局部变量.表中的域. ...
- 生产者消费者问题--synchronized
# 代码 public class App { public static void main(String[] args) { Depot depot = new Depot(100); Produ ...
- kotlin项目开发基础之gradle初识
在Android Studio推出之后默认的打包编译工具就变为gradle了,我想对于一名Android程序员而言没人不对它知晓,但是对于它里面的一些概念可能并不是每个人都了解,只知道这样配置就ok了 ...
- 手动从Spring中获取指定对象
1.创建ApplicationContext对象: ApplicationContext context = new FileSystemXmlApplicationContext("D:/ ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(14)|Impls & Traits实现与特征]
[易学易懂系列|rustlang语言|零基础|快速入门|(14)] 有意思的基础知识 Impls & Traits实现与特征 我之前说到的struct结构体,其实就类似于面向对象语言中的类cl ...