[LeetCode]题解(python):115-Distinct Subsequences
题目来源:
https://leetcode.com/problems/distinct-subsequences/
题意分析:
给定字符串S和T,判断S中可以组成多少个T,T是S的子串。
题目思路:
这是一个动态规划的问题。设定ans[i][j]为s[:i] 组成t[:j]的个数。那么动态规划方程为,if s[i - 1] == t[j - 1]: ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j];else: ans[i][j] = ans[i - 1][j]。
代码(python):
class Solution(object):
def numDistinct(self, s, t):
"""
:type s: str
:type t: str
:rtype: int
"""
m,n = len(s),len(t)
ans = [[0 for i in range(n+1)] for i in range(m+1)]
for i in range(m + 1):
ans[i][0] = 1
for i in range(1,m + 1):
for j in range(1,n + 1):
if s[i - 1] == t[j - 1]:
ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j]
else:
ans[i][j] = ans[i-1][j]
return ans[m][n]
[LeetCode]题解(python):115-Distinct Subsequences的更多相关文章
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- [LeetCode] 115. Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- Java for LeetCode 115 Distinct Subsequences【HARD】
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
- leetcode 115 Distinct Subsequences ----- java
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [leetcode]115. Distinct Subsequences 计算不同子序列个数
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- 115. Distinct Subsequences
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- LeetCode之“动态规划”:Distinct Subsequences
题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...
- 115. Distinct Subsequences (String; DP)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- JQuery 操作input
获取选中的值 获取一组radio被选中项的值 var item = $('input[@name=items][@checked]').val(); 获取select被选中项的文本 var item ...
- .aspx.cs传值与取值
1:.aspx中post传值 $.post("ABP_ExchangeRatelz.aspx", { option: "isdelete", Ori_Curre ...
- Linux 文件内容转码
文件内容的转换: iconv -f GB2312 -t UTF-8 gb1.txt >gb2.txt-f, –from-code=名称 原始文本编码-t, –to-code=名称 输出编码-o, ...
- jsf小例子
有人问我用过jsf没? 当时没有用过,就看了一下: 写了一个小例子 JSF和struts2 差不多的,都有一些配置和跳转 struts2的action配置和JSF的faces-config.xm ...
- HDU 5573 Binary Tree(找规律)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5573 题意:给你一个完全二叉树,节点为自然数的排列(第一行1,第二行2 3,第三行4 5 6 7... ...
- Labview中引用,属性节点,局部变量之间的区别
Labview中引用,属性节点,局部变量之间的区别 在Labview中我们经常会碰到这样几个概念,那就是控件的引用,属性节点以及局部变量,他们之间到底有哪些区别呢? 首先谈 ...
- html5 canvas 运行起来绝对让你震撼!
从一个大神那看到的,拷贝过来跟大家分享下! html <canvas></canvas> *{margin:0;padding:0;}body{background:#222; ...
- java中对于JSON 的处理 fastjson gson 系统自带的JSON 的选择
从2月初到8月末,经历了一段痛苦的经历,现在总算感觉已经走出来了,经历那事之后 感觉对人与人之间的感情看的更透了,人也没那么浮躁了: 说实话 以前从来不知道鸟叫有多好听,现在突然觉的大自然真的很美,放 ...
- 个人收集资料整理-WinForm
[2016-03-23 20:29:56] 别人收集常用: http://www.cnblogs.com/babycool/p/3541192.html
- Linux 关机
1. shutdown shutdown -h now 立刻关机 shutdown -h 22:50 22:50 分的时候关机 shutdown -h +10 10分钟后关机 shutdwon -r ...