滚动数组:

/*****
下标从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. (转)java并发编程:CopyOnWriteArrayList

    原文链接:http://ifeve.com/java-copy-on-write/ Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容, ...

  2. python jdbc连接 oracle 数据库

    准备写一个代码生成的小工具自己用,第一步,连接数据库 import jaydebeapi url = 'jdbc:oracle:thin:@192.168.0.13:1521:JGD' user = ...

  3. C语言字符串操作小结

    1)字符串操作strcpy(p, p1) 复制字符串strncpy(p, p1, n) 复制指定长度字符串strcat(p, p1) 附加字符串strncat(p, p1, n) 附加指定长度字符串s ...

  4. pamamiko的学习笔记

    pamamiko的学习笔记 Paramiko包含两个核心组件,一个为SSHClient类,另一个为SFTPClient类, 一,paramiko的连接有两种方式,一种是通过paramiko.SSHCl ...

  5. Python数据驱动DDT的应用

    在开始之前,我们先来明确一下什么是数据驱动,在百度百科中数据驱动的解释是:数据驱动测试,即黑盒测试(Black-box Testing),又称为功能测试,是把测试对象看作一个黑盒子.利用黑盒测试法进行 ...

  6. Mongodb操作3-可视化工具使用

    1.无密码登录 1.创建连接 输入ip后 先测试在链接 2.有密码登录 设置密码 1.选择主数据库 >>>use admin # 第一步 选择主数据 switched to db a ...

  7. 【CF 463F】Escape Through Leaf

    题意 给你一棵 \(n\) 个点的树,每个节点有两个权值 \(a_i,b_i\). 从一个点 \(u\) 可以跳到以其为根的子树内的任意一点 \(v\)(不能跳到 \(u\) 自己),代价是 \(a_ ...

  8. Django-多对多建表与Form表单

    一.多对多建表的三种创建方式: 1.全自动型:(一般情况下使用) class Book(models.Model): title = models.CharField(max_length=32) a ...

  9. WebService帮助类改良版,支持多webservice

    帮助类代码 using System; using System.CodeDom; using System.CodeDom.Compiler; using System.Collections.Ge ...

  10. CART 分类与回归树

    from www.jianshu.com/p/b90a9ce05b28 本文结构: CART算法有两步 回归树的生成 分类树的生成 剪枝 CART - Classification and Regre ...