【leetcode】Distinct Subsequences(hard)
Given a string S and a string T, count the number of distinct subsequences of T in S.
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).
Here is an example:
S = "rabbbit", T = "rabbit"
意思是指可以通过多少种方式删除S的一部分字母使得S变为T。
思路:真高兴,又做出来了~~
用ways[m][n]存储 S[0~m-1]变为T[0~n-1]的方式
那么 ways[i][j] = ways[i-1][j] //扔掉S[i-1]
+((S[i-1] == T[j-1]) ? ways[i-1][j-1] : 0); //当前S与T的字母匹配,则需加上S[0~m-2]变为T[0~n-2]的方式数
class Solution {
public:
int numDistinct(string S, string T) {
int slen = S.length();
int tlen = T.length();
if(slen < tlen) return ;
vector<vector<int>> ways(slen + , vector<int>(tlen + , ));
ways[][] = ;
for(int i = ; i < slen + ; i++)
{
ways[i][] = ; //若T没有字母那么只有一种方式令S变为T:删除S全部的字母
}
for(int i = ; i < slen + ; i++)
{
for(int j = ; j < tlen + ; j++)
{
ways[i][j] = ways[i-][j] //扔掉当前的
+((S[i-] == T[j-]) ? ways[i-][j-] : ); //当前S与T的字母匹配
}
}
return ways[slen][tlen];
}
};
看了看别人的答案,发现我们的代码几乎是一模一样,难道说题做多了大家的风格都一样了吗?
有个优化的方法,因为在计算ways[i][j]时,只用到了ways[i-1]行的信息,所以没有必要存储所有的历史信息,只要存上一行的就好。
/**
* Further optimization could be made that we can use only 1D array instead of a
* matrix, since we only need data from last time step.
*/ int numDistinct(string S, string T) {
int m = T.length();
int n = S.length();
if (m > n) return ; // impossible for subsequence vector<int> path(m+, );
path[] = ; // initial condition for (int j = ; j <= n; j++) {
// traversing backwards so we are using path[i-1] from last time step
for (int i = m; i >= ; i--) {
path[i] = path[i] + (T[i-] == S[j-] ? path[i-] : );
}
} return path[m];
}
【leetcode】Distinct Subsequences(hard)的更多相关文章
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】深搜DFS(共85题)
[98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- 理解CSS3中的background-size(对响应性图片等比例缩放)
理解CSS3中的background-size(对响应性图片等比例缩放) 阅读目录 background-size的基本属性 给图片设置固定的宽度和高度的 固定宽度400px和高度200px-使用ba ...
- zepto-selector.js简单分析
zepto 的selector模块是对jquery扩充选择器(jquery-selector-extensions)的部分实现.比如这样的选择方法:$('div:first') 和 el.is(':v ...
- (免量产,免格式化)手动将PE安装到移动硬盘/U盘或无系统硬盘!
在一台没有装系统的电脑上,只要把XP系统启动文件,及引导菜单文件(ntldr,boot.ini,”bootfont.bin这个可有可无,主要作用是显示中文菜单”)和GRUB引导文件和PE系统文件复制到 ...
- Perl 正则表达式
匹配:m/<regexp>;/ (还可以简写为 /<regexp>;/ ,略去 m)替换:s/<pattern>;/<replacement>;/转化: ...
- ThinkPHP3.2.3 安装教程
本文以 Windows 平台为例 安装前准备:Windows操作系统的电脑,php编程环境(配置好了Apache.MySql.php).推荐wampserver. 待安 ...
- cf#305 Mike and Foam(容斥)
C. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- hadoop之根据Rowkey从HBase中查询数据
1.Hbase 根据rowkey 查询 conf的配置信息如下: conf = new Configuration(); conf.set("hbase.zookeeper.quorum&q ...
- Xcode如何找到默认的生成路径?
我最近刚刚入门ObjectiveC,在研习<Objective C程序设计(第6版)>一书. 今天看到有关文件和归档的章节,但是我对XCode的生成文件路径并不了解,然后,在调试代码的时候 ...
- 用css布局的方法实现如果字符超过一定长度就显示成省略号
以前实现这种效果需要在程序里判断字符的长度,如果长度大于多少个字符,就截取字符,用省略号代替,而且是在服务器处理的,现在只需要用css的属性来操作,简单.实用.节省性能.不用做过多的程序判断.节约开发 ...
- SEO 百度后台主动推送链接
实践步骤,先用爬虫程序将本网站的所有连接爬取出来,再用python文件处理程序把爬虫来的东东整理成一行一个链接的文本格式.再用postman接口测试工具,使用post方式,将所有的链接post过去,这 ...