滚动数组:

/*****
下标从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不同的子序列的更多相关文章

  1. Java实现 LeetCode 115 不同的子序列

    115. 不同的子序列 给定一个字符串 S 和一个字符串 T,计算在 S 的子序列中 T 出现的个数. 一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字 ...

  2. Leetcode 115.不同的子序列

    不同的子序列 给定一个字符串 S 和一个字符串 T,计算在 S 的子序列中 T 出现的个数. 一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串.(例 ...

  3. LeetCode 115.不同的子序列 详解

    题目详情 给定一个字符串 S 和一个字符串 T,计算在 S 的子序列中 T 出现的个数. 一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串.(例如, ...

  4. [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 ...

  5. [LeetCode 115] - 不同子序列(Distinct Subsequences)

    问题 给出字符串S和T,计算S中为T的不同的子序列的个数. 一个字符串的子序列是一个由该原始字符串通过删除一些字母(也可以不删)但是不改变剩下字母的相对顺序产生的一个新字符串.如,ACE是ABCDE的 ...

  6. [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 ...

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

  8. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  9. [LeetCode] Increasing Subsequences 递增子序列

    Given an integer array, your task is to find all the different possible increasing subsequences of t ...

随机推荐

  1. centos 7 安装 redis-5.0.5

    [root@localhost ~]# yum -y install gcc make [root@localhost ~]# wget http://download.redis.io/releas ...

  2. Centos修改IP的两种方式

    nmtui命令调出修改窗口:nmtui    重启网卡:/etc/init.d/network restart 或者:service network restart 方式二: 进入网络配置文件目录 首 ...

  3. javaweb 一个坑—— writer必须放在response相关设置后面

    https://blog.csdn.net/weixin_33962621/article/details/86454414

  4. web性能优化-浏览器渲染原理

    在web性能优化-浏览器工作原理中讲到,浏览器渲染是在renderer process中完成的. 那我们来看下renderer process究竟干了什么? Renderer Process包含的线程 ...

  5. windows笔记本命令行方式建立wifi热点

    建立热点: @echo off netsh wlan set hostednetwork mode=allow netsh wlan set hostednetwork ssid=热点名 key=密码 ...

  6. 基于树莓派2代的DIY无线路由器

    最近手上多了一个树莓派2代,于是折腾就这么开始了. 因为总是得要个显示屏或者路由器或者插根网线才能玩,有点麻烦,所以有了此文. 设备清单: 树莓派2代 EDUP EP-N8508GS无线网卡(USB) ...

  7. Spring Boot干货系列:(十二)Spring Boot使用单元测试(转)

    前言这次来介绍下Spring Boot中对单元测试的整合使用,本篇会通过以下4点来介绍,基本满足日常需求 Service层单元测试 Controller层单元测试 新断言assertThat使用 单元 ...

  8. 输出1~n中1的个数

    //输出1~n中1的个数,如f(1)=1,f(13)=6.通过测试,bymyself public class FindOnes{ public static void main(String arg ...

  9. sqlite3创建自增主键,以及清空表使自增列归零

    1.创建自增主键 CREATE TABLE tb_python (ID INTEGER PRIMARY KEY AUTOINCREMENT,TITLE TEXT,URL TEXT); 2.清空表 SQ ...

  10. 【Winform-ComboBox】实现ComboBox下拉框与数据库的绑定

    实现效果如下: 1.设计窗体 下拉框的名称cmbName 2.连接数据库 DBHelper类代码: class DBHelper { /// <summary> /// 创建静态连接字符串 ...