LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静
Distinct Subsequences
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"
Return 3
.
其实这道题本来不是很难,因为一看就是动态规划。结果笨娃(哎,真是笨娃啊)搞这个递推式搞了很久。所以记录一下。
Instinctly,(真心发现既然是dp,就往这方面想就是)假设S取前面i个字符(最后一个index是i - 1), T取前面j个字符(最后一个index是j - 1),那么在前i个字符中,有序列的个数num[i][j]的公式应该怎么写呢?
首先,如果S[i - 1] 不等于T[j - 1],那么num[i][j] = num[i - 1][j]。不难理解啊,就是S往前缩一个呗,反正也匹配不了T[j - 1]是不是?
如果S[i - 1]等于T[j - 1], 那么,num[i][j]应该是: num[i - 1][j - 1] + num[i - 1][j]。
为啥呢?
num[i - 1][j - 1]: S中还没到i的同志们在翘首盼望着i ,同样T中的乡亲们也在等待j 。符合条件的S[i - 1]一到达,他们就自然加入到num[i][j]的队伍中了,如下图所示。
num[i - 1][j]: S还没到i,但其中一些同志们已经满足T[0: j-1]。符合条件的S[i - 1]到达,他们需要也加入到num[i][j]的队伍中,如下图所示。
(是不是觉得楼主疯了)
聪明的同学肯定要问了,但是初始状态大家都是0,没见到+1啊。这里有个特殊的初始化,就是num[i][0] = 1。这好像在说,空串始终匹配整个串S
代码如下:
public int numDistinct(String S, String T) {
if (T.length() > S.length()) {
return 0;
}
int[][] num = new int[S.length() + 1][T.length() + 1];
for (int i = 0; i <= S.length(); i++) {
num[i][0] = 1;
}
for (int i = 1; i <= S.length(); i++) {
for (int j = 1; j <= Math.min(i, T.length()); j++) {
if (S.charAt(i - 1) == T.charAt(j - 1)) {
num[i][j] = num[i - 1][j - 1] + num[i - 1][j];
} else {
num[i][j] = num[i - 1][j];
}
}
} return num[S.length()][T.length()];
}
LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静的更多相关文章
- 【leetcode刷题笔记】Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】940. Distinct Subsequences II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
- Distinct Subsequences ——动态规划
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- leetcode -day 15 Distinct Subsequences
1. Distinct Subsequences Given a string S and a string T, count the number of distinct subsequen ...
- 【LeetCode】114. Distinct Subsequences
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- 【leetcode】940. Distinct Subsequences II
题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...
随机推荐
- HTTPS连接的前几毫秒发生了什么——Amazon HTTPS案例分析
转自: http://blog.jobbole.com/48369/ 提示:英文原文写于2009年,当时的Firefox和最新版的Firefox,界面也有很大改动.以下是正文. 花了数小时阅读了如潮的 ...
- GridControl控件的数据显示的样式控制(转)
如上两图所示,Dev列表控件GridControl默认的格式并没有渐变变色效果,显示的日期数据,也是“yyyy-MM-dd”的格式,而非“yyyy-MM-dd HH:mm:ss”即使对于后面有长格式的 ...
- Eclipse 启动Tomcat 超时报错的解决方案
在用eclipse开发项目 用tomcat发布项目的时候 会提示超时, Server Tomcat v7.0 Server at localhost was unable to start wit ...
- cmd获取系统时间
查看时间格式: echo %date% echo %time% 通过%date:~5,2%来组合得出当前日期,组合的效果为yyyymmdd,date命令得到的日期格式默认为yyyy-mm-dd,通过% ...
- common-pool2对象池(连接池)的介绍及使用
我们在服务器开发的过程中,往往会有一些对象,它的创建和初始化需要的时间比较长,比如数据库连接,网络IO,大数据对象等.在大量使用这些对象时,如果不采用一些技术优化,就会造成一些不可忽略的性能影响.一种 ...
- cni 添加网络 流程分析
cnitool: Add or remove network interfaces from a network namespace cnitool add <net> <netns ...
- LeetCode题解-----Majority Element II 摩尔投票法
题目描述: Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The a ...
- css3的媒体查询(Media Queries)
我今天就总结一下响应式设计的核心CSS技术Media(媒体查询器)的用法. 先看一个简单的例子: <link rel="stylesheet" media="scr ...
- Linux搭建python环境中cx_Oracle模块安装遇到的问题与解决方法
安装或使用cx_Oracle时,需要用到Oracel的链接库,如libclntsh.so.11.1,否则会有各种各样的错误信息. 安装Oracle Instant Client就可得到这个链接库,避免 ...
- git 添加文件
git 添加文件三步骤 git add filename git commit -m 'remarks' git push origin master