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 ...
随机推荐
- 5.(基础)tornado异步
终于到了传说中的异步了,感觉异步这个名字听起来就很酷酷的,以前还不是多擅长Python时,就跑去看twisted的源码,结果给我幼小的心灵留下了创伤.反正包括我在内,都知道异步编程很强大,但是却很少在 ...
- hive不分区增量更新
insert overwrite table ods.zeg_so select *,case when zsm.id is not null then cast(current_timestamp ...
- 集合(四) Hashtable
2.Hashtable Hashtable,顾名思义,哈希表,本来是已经被淘汰的内容,但在某一版本的Java将其实现了Map接口,因此也成为常用的集合类,但是hashtable由于和hashmap十分 ...
- 【洛谷P4173】残缺的字符串
题目大意:给定一个文本串和一个模板串,串中含有通配符,求文本串中有多少个位置可以与文本串完全匹配. 题解:利用卷积求解字符串匹配问题. 通配符字符串匹配的数值表示为 \[\sum\limits_{i ...
- JavaWEB开发05_Bootstrap
上次课内容:什么JQ : write less do more 写更少的代码,做更多的事情 javascript函数库 基本选择器: ID选择器: #ID名称 类选择器: .类名 元素选择 ...
- VS开发框架DevExtreme v19.1全解析!Windows资源管理器UX值得拥有
行业领先的.NET界面控件DevExpress 正式发布了v19.1版本,本文将以系列文章的方式为大家介绍DevExtreme Complete Subscription v19.1中全新发布的文件管 ...
- k8sReplicaSet控制器
一.ReplicaSet概述 简称RS,是pod控制器类型的一种实现,用于确保由其管控的pod对象副本数在任一时刻都能精确满足期望的数量.ReplicaSet控制器资源启动后会查找集群中匹配其标签选择 ...
- php核心:代际划分/运行环境等
不想在python一棵树上吊死,不是所有Python就一定得会人工智能大数据,so...... 何谓php?早期:personel homepage 是也!后期Hypertext preprocess ...
- SVN各种错误提示产生原因及处理方法大全
SVN各种错误提示产生原因及处理方法大全 SVN各种错误提示产生原因及处理方法大全 1. svn: Server sent unexpected return value (500 Internal ...
- vue自定义指令,自动调用下载的方法
directives: { clickDown: { inserted (el, binding, item) { if (+binding.value.item.fromId === +item.c ...