2020-01-03 13:29:04

问题描述:

问题求解:

经典的动态规划题目,一般来说dp题目是递推关系公式难想,但是实际代码量还是比较少的。

有尝试过dfs来做,但是由于时间复杂度是指数级别的,所以会TLE。

    public int numDistinct(String s, String t) {
int n1 = s.length();
int n2 = t.length();
int[][] dp = new int[n2 + 1][n1 + 1];
for (int i = 0; i <= n1; i++) dp[0][i] = 1;
for (int i = 1; i <= n2; i++) {
for (int j = 1; j <= n1; j++) {
if (t.charAt(i - 1) == s.charAt(j - 1)) dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1];
else dp[i][j] = dp[i][j - 1];
}
}
return dp[n2][n1];
}

  

动态规划-Distinct Subsequences的更多相关文章

  1. 动态规划——Distinct Subsequences

    题目大意:给定字符串S和T,现在从S中任选字符组成T,要求输出方案个数. Example 1:Input: S = "rabbbit", T = "rabbit" ...

  2. LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静

    Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of  ...

  3. Distinct Subsequences ——动态规划

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  4. LeetCode之“动态规划”:Distinct Subsequences

    题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...

  5. Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划

    Given a string S and a string T, count the number of distinct subsequences ofT inS. A subsequence of ...

  6. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  7. Leetcode Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  8. LeetCode(115) Distinct Subsequences

    题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...

  9. [Leetcode][JAVA] Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

随机推荐

  1. 設定MacOS 終端機環境 (Homebrew/zsh/oh-my-zsh/iTerm2)

    安裝Homebrew 1 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/in ...

  2. RocketMQ 单机版安装并测试

    一.安装maven 1.下载maven,http://maven.apache.org/download.cgi,并解压: -bin.tar.gz 2.修改系统环境变量,并验证: vim /etc/p ...

  3. Dream权限追踪系统<=2.0.1 重安装漏洞

    在./install/install.php中 if(file_exists('lock.txt')){ echo '系统已安装,请不要重复安装!如需安装,请删除install文件夹下的lock.tx ...

  4. 什么是SNAT

    SNAT是源地址转换,其作用是将ip数据包的源地址转换成另外一个地址,可能有人觉得奇怪,好好的为什么要进行ip地址转换啊,为了弄懂这个问题,我们要看一下局域网用户上公网的原理,假设内网主机A(192. ...

  5. 【转载】java 获取路径的各种方法

    转载只供个人学习参考,查看请前往原出处:http://www.cnblogs.com/guoyuqiangf8/p/3506768.html 主要方法有: (1).request.getRealPat ...

  6. flask 中文乱码

    返回的json数据中包含中文,但前端显示乱码,添加了 app.config['JSON_AS_ASCII'] = False仍然没有解决, app.config['JSONIFY_MIMETYPE'] ...

  7. CSS3详解:border color

    继续我们的 ,大家觉得怎么样呢?

  8. PostCSS的插件 -- 关于vue rem适配布局方案

    PostCSS的插件 作用:用于自动将像素单元生成rem单位 记以下三种 postcss-plugin-px2rem postcss-pxtorem postcss-px2rem 任选一种,最近大家推 ...

  9. java算法--链表

    虽然这个文章看着很多,但是大多是对于细节的讲解,如果想要快速了解,可以直接观看末尾代码.上面的代码内容都是来自于文章末尾的代码. 很重要的算法,也是比较简单的算法. 但是在java中,因为不存在c和c ...

  10. 【已解决】HDFS节点已经启动,但不能访问50070 ?

    问题描述 通过start-dfs.sh启动了三个节点 但无法通过IP访问50070端口 问题分析 1.可能是防火墙没关,被拦截了 果然,防火墙没关 再将防火墙设为开机不启动 systemctl dis ...