leetCode Distinct Subsequences 解题分享
来源:互联网 发布:javascript中map.get() 编辑:程序博客网 时间:2024/05/17 00:19
原题:https://oj.leetcode.com/problems/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
.
首先说明一点,这题不是独立完成,参考了很多篇文章的解法完成的。之所以要在这里分享出来,是想补充一些在其他文章里面没有看到的或者我认为描述的不够清楚的内容。供跟我遇到同样问题或者困惑的同学们参考。
1)解这道题用了将近2个小时。其中头一个小时基本用在对题意理解的纠结上了!我不知道是我的英语太差还是怎么回事,直到现在我都认为题意描述有重大歧义:
题干部分:the number of distinct subsequences of T in S.-----我是这样断句的:“subsequences of T” ,“in S” 。再结合后面的“A subsequence of a string”,“"ACE"
is a subsequence of "ABCDE"
” 等等,综上我认为是要看S里面含有多少个T的subsequences 。 结合给出的例子,我一开始认为答案的3是这么来的:1--rab 2--bb 3--bit
因为题意理解上的错误,导致的结果是首先自己思考的时候,难度加大。然后再看其他人解题代码的时候,完全无法理解。还有看其他有些人的分析过程,更是完全不知所云,就在这样的纠结过程中度过了1个小时。
后来经过看多个人的思路分析加上反复的AC代码阅读,终于知道并确认了这题是要找出S的subsequences!!T作为一个整体,是不能拆分的!
正确题意是:让S经过删减字符,变成T,问有多少种不同的方案........相应例子中的结果是3是说把S中的三个d分别去掉可以得到T
真他大爷的坑爹啊。
2)理解了正确题意的基础上,再看分析过程,就容易些了,我不多描述了,转载一篇文章,个人认为这篇文章描述的相对易懂和清晰,最重要的是相写的比较负责,能看出是真想分享解题思路的诚意:
http://www.cnblogs.com/ganganloveu/p/3836519.html
没有任何对其他人不尊敬的意思,很多人特别是程序员,他们在描述问题的时候可能觉得自己能懂就行,经常寥寥几句完工,关键点完全说不出来,这样的思路说了还不如没说,有时候让人更加糊涂。觉得既然写了,就写的清楚一点,别让其他人想参考一下的时候,看到你写的每句话都得理解半天!
AC代码:
public class DistinctSubsequences { public int numDistinct(String S, String T) { if(S == null) { if(T == null || T.length() == 0){ return 1; }else{ return 0; } } else { if(T == null || T.length() == 0) { return 1; } else { if(S.length() < T.length()) return 0; } } int[][] dp = new int[S.length()+1][T.length()+1]; for(int i=0;i<=S.length();i++) { dp[i][0] = 1; } for(int i=1;i<=S.length();i++) { for(int j=1;j<=i&&j<=T.length();j++) { dp[i][j] = dp[i-1][j]; if(S.charAt(i-1) == T.charAt(j-1)) { dp[i][j] += dp[i-1][j-1]; } } } return dp[S.length()][T.length()]; }}
- leetCode Distinct Subsequences 解题分享
- Leetcode Distinct Subsequences 解题报告
- [LeetCode]Distinct Subsequences,解题报告
- leetcode解题笔记-Distinct Subsequences
- 【LeetCode】Distinct Subsequences 解题报告
- [Leetcode] Distinct Subsequences 解题报告
- [leetcode] 115. Distinct Subsequences 解题报告
- [Leetcode] 115. Distinct Subsequences 解题报告
- Distinct Subsequences 解题报告
- Leetcode 115. Distinct Subsequences 计算子串 解题报告
- LeetCode Distinct Subsequences
- [LeetCode]Distinct Subsequences
- [Leetcode] Distinct Subsequences *
- [leetcode] Distinct Subsequences
- [LeetCode] Distinct Subsequences
- [Leetcode] Distinct Subsequences
- leetcode 111: Distinct Subsequences
- LeetCode Distinct Subsequences
- BZOJ 1500 NOI2005 维修数列 Splay
- GRUB
- 为什么编辑器上传不了图片?
- jieba
- C++中引用符号“&”的作用总结
- leetCode Distinct Subsequences 解题分享
- 为什么编辑器加载不了图片?
- 详解神秘Lin
- C++ 使用JsonCpp
- 【完整版】需要处理很长的商品图片,但是却被压缩到3500
- 设置ssh登陆的时候不用输入密码的方法:
- 发散按钮浅谈
- 【Tools.Eclipse】Aptana Studio
- linux系统文件解压缩命令总结