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 ...
随机推荐
- vs编译项目报错:The OutputPath property is not set for this project
今天使用VS2008编译项目时报错: The OutputPath property is not set for this project. Please check to make sure t ...
- gitlab和jenkins的安装及使用
gitlab 准备: 最少4G内存 先安装docker软件包然后使用docker search gitlab 查找镜像然后使用docker pull 镜像名:标签名 下载镜像启动容器: docker ...
- openstack Rocky系列之keystone:(二)keystone中API注册
主要说一下initialize_application中的application_factory def loadapp(): app = application.application_factor ...
- ZOJ red black tree
#include <bits/stdc++.h> #define fi first #define se second #define lson l,m,rt<<1 #defi ...
- C#基础进阶
观看C#高级教程进行学习.巩固基础,进阶学习. 1.委托 把方法当做参数来传递就是委托.委托的关键字是delegate. class Program { private delegate string ...
- Linux下普通用户与root用户之间的互相切换
只是切换root身份,环境仍是普通用户shell su.su -.su root 按照提示输入相应的root密码,就可登录到root权限下. 用户和shell环境都切换为root sudo -i.su ...
- 零拷贝的原理及Java实现
在谈论Kafka高性能时不得不提到零拷贝.Kafka通过采用零拷贝大大提供了应用性能,减少了内核和用户模式之间的上下文切换次数.那么什么是零拷贝,如何实现零拷贝呢? 什么是零拷贝 WIKI中对其有如下 ...
- 初识linux(简单命令)
之前一直搞不懂,为什么全是命令行的linux系统这么多公司都在用,当你看不懂那一行行命令时你一定会和我一样觉得头大.但当你学习了命令再结合桌面版觉得linux还是挺不错的
- 实现分享功能(分享到qq空间,新浪微博)
//分享QQ好友 function qq(title,url,pic) { var p = { url: 'http://test.qichey ...
- js 多个三目运算符优先级
读JS代码遇到一段看不懂运算优先级的代码,如下 var BrowserSys = {}; var ua = navigator.userAgent.toLowerCase(); var s; (s = ...