LeetCode(115) 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
.
分析
- 定义二维数组dp[i][j]为字符串s(0,i)变换到t(0,j)的变换方法。
- 如果S[i]==T[j],那么dp[i][j] = dp[i-1][j-1] + dp[i-1][j]。意思是:如果当前S[i]==T[j],那么当前这个字母即可以保留也可以抛弃,所以变换方法等于保留这个字母的变换方法加上不用这个字母的变换方法。
- 如果S[i]!=T[i],那么dp[i][j] = dp[i-1][j],意思是如果当前字符不等,那么就只能抛弃当前这个字符。
- 递归公式中用到的dp[0][0] = 1,dp[i][0] = 0(把任意一个字符串变换为一个空串只有一个方法)
AC代码
class Solution {
public:
/*用删除的方法将串s变换到t,计算变换方法数*/
int numDistinct(string s, string t) {
if (s.empty() || t.empty())
return ;
else if (s.length() < t.length())
return ;
else
{
//动态规划
int ls = s.length(), lt = t.length();
/*保存由字符串s(0,i) --> t(0,j)的方法数*/
vector<vector<int> > dp(ls + , vector<int>(lt + , ));
dp[][] = ;
for (int i = ; i < ls; ++i)
{
/*s(0,i) 转换为 t(0)的方法数为1*/
dp[i][] = ;
}//for
for (int i = ; i <= ls; ++i)
{
for (int j = ; j <= lt; ++j)
{
/*首先不管当前字符是否相同,为dp[i][j]赋初值*/
dp[i][j] = dp[i - ][j];
if (s[i-] == t[j-])
{
/*如果s和t的当前字符相同,有两种选择保留或不保留*/
dp[i][j] += dp[i - ][j - ];
}//if
}//for
}//for
return dp[ls][lt];
}
}
};
LeetCode(115) Distinct Subsequences的更多相关文章
- LeetCode(115):不同的子序列
Hard! 题目描述: 给定一个字符串 S 和一个字符串 T,计算在 S 的子序列中 T 出现的个数. 一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字 ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
随机推荐
- (转载)iOS 极光推送SDK 集成指南
iOS SDK 集成指南 使用提示 本文匹配的 SDK版本:r1.2.5 以后. 查看最近更新了解最新的SDK更新情况. 产品功能说明 极光推送(JPush)是一个端到端的推送服务,使得服务器端消息能 ...
- SQL联合查询两个表的数据
刚有个项目,需要查询水位数据表中的水位信息,及查询降雨量表中统计时段降雨量的数据,以计算出日降雨量,而且时段是前一天8时到后一天8时总共24个小时. 两个子查询: 1.根据当前时间判断统计前天8时到今 ...
- tcl学习
variables(变量) 语法:set varname value 例如:set a 5 注意:大小写敏感,任意长度,任意字符 使用之前无需申明 substitution(替换) 1 变量值替换 $ ...
- asp.net 琐记
Page的AutoEventWireup作用是是否引发PreInit Load PreRender Unload几个页面处理流程事件 和控件的事件处理函数无关
- java中的 FileWriter类 和 FileReader类的一些基本用法
1,FileWriter类(字符输出流类) |--用来写入字符文件的便捷类.此类的构造方法假定默认字符编码和默认字节缓冲区大小都是可接受的.要自己指定这些值,可以先在 FileOutputStream ...
- Mac OS X的空间去哪儿了
记得有事儿没事儿看下,/cores/目录的大小. cores目录是存放程序dump的数据,对于绝大部分人时是没有用的.可以删除.如果遇到坑爹的程序,一直生成dump文件,硬盘瞬间就爆满了. 典型:An ...
- android之handle
Android中异步消息处理主要由四个部分组成,Message.handler.messageQueue和looper. 1.message message是线程之间传递的消息,他可以在内部携带少量的 ...
- css总结
1. css中的role属性 html 里面的 role 本质上是增强语义性,当现有的HTML标签不能充分表达语义性的时候,就可以借助role来说明.通常这种情况出现在一些自定义的组件上,这样可增强组 ...
- WCF通过SVCUtil.exe生成客户端代理类和配置文件(转)
WCF服务调用通过两种常用的方式: 1:一种是借助代码生成工具SvcUtil.exe或者添加服务引用的方式. 2:一种是通过ChannelFactory直接创建服务代理对象进行服务调用. 本文只针对通 ...
- JS编写背景图切换
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...