【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动画由浅入深总结
阅读目录 一:过渡动画---Transitions 二:Animations功能 三:translate(tx,ty) 四:scale(x,y) 五:rotate(x): 5.1:skew(x,y): ...
- sql移除换行回车符号 \r\n
--移除回车符 update master_location SET street_number = REPLACE(street_number, CHAR(13), '') --移除换行符 upda ...
- PHP的 Mysqli扩展库的多语句执行
$mysqli->multi_query($sqls); 执行多个sql语句,返回true/false 有结果集时,使用 $mysqli->store_result(); 来获取结 ...
- CSS样式案例(1)-文字的排版
本篇介绍的是小窗文字内容的排版,通过该篇文章可以让小伙伴们熟悉以下几个知识点: word-space.overflow.text-overflow. 最终的展示效果如下: 参考步骤: 1. 建立htm ...
- 用MVC的辅助方法自定义了两个控件:“可编辑的下拉框控件”和“文本框日历控件”
接触MVC也没多长时间,一开始学的时候绝得MVC结构比较清晰.后来入了门具体操作下来感觉MVC控件怎么这么少还不可以像ASP.net form那样拖拽.这样设计界面来,想我种以前没学过JS,Jquer ...
- 关于viewport
最近无聊的很,买了本教材,学习响应式网站设计. 因为有多年css的编程基础,前面的媒介查询学的很顺利.当学到viewport这个mata标签的时候,教程讲的比较简单. 今天,百度了不少资料,基本搞清楚 ...
- DEV控件Grid显示行号
DEV控件Grid的显示行号需要通过一个事件来设置,具体设置代码为: private void gridView1_CustomDrawRowIndicator(object sender, DevE ...
- Java设计模式 之 代理模式
所谓的代理模式就是为其它类或对象提供一个代理以控制对这个对象的访问.那么常见的代理有远程代理,虚拟代理,保护代理,智能代理. 1. 远程代理:为一个不同地址空间的对象提供一个本地代理对象. 2. 虚拟 ...
- 架设 OpenLDAP服务器
OpenLDAP是一个开放源代码的软件,可以免费获取使用,其主页地址是:http://www.openldap.org/.在RHEL 6上安装OpenLDAP还需要libtool-ltdl-2.2.6 ...
- << CocoaPods安装和使用教程 >>github code4app以及cocoachina 苹果官方文档
developer.apple.com 英文搜索各个技术的官方介绍文档, 前提是英文过关 cocoachina ios最新新闻, 信息 code4app上有许多组件 http://www.code4a ...