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 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问题,弄清递推公式即可,参考Distinct Subsequences@LeetCode ,JAVA实现如下:
public int numDistinct(String s, String t) {
if (t.length() == 0)
return 1;
int[] dp = new int[t.length() + 1];
dp[0] = 1;
for (int i = 0; i < s.length(); i++) {
int last = 1;
for (int j = 0; j < t.length(); j++) {
if (dp[j] == 0)
break;
int temp = dp[j + 1];
if (s.charAt(i) == t.charAt(j))
dp[j + 1] += last;
last = temp;
}
}
return dp[t.length()];
}
Java for LeetCode 115 Distinct Subsequences【HARD】的更多相关文章
- 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 ...
- [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 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
- Leetcode#115 Distinct Subsequences
原题地址 转化为求非重路径数问题,用动态规划求解,这种方法还挺常见的 举个例子,S="aabb",T="ab".构造如下地图("."表示空位 ...
- Java for LeetCode 207 Course Schedule【Medium】
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 146 LRU Cache 【HARD】
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- Java for LeetCode 072 Edit Distance【HARD】
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Java for LeetCode 097 Interleaving String 【HARD】
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
随机推荐
- 【IntelliJ IDEA】spring boot项目在idea实现自动部署
转载参考自:https://www.cnblogs.com/winner-0715/p/6666579.html spring-boot-devtools是一个为开发者服务的一个模块,其中最重要的功能 ...
- 【python】redis基本命令和基本用法详解
[python]redis基本命令和基本用法详解 来自http://www.cnblogs.com/wangtp/p/5636872.html 1.redis连接 redis-py提供两个类Redis ...
- 【AngularJS】【02】AngularJS应用骨架
※文件引自OneDrive,有些人可能看不到
- 出现“Windows资源管理器已停止工作”错误
出现"Windows资源管理器已停止工作"错误 什么是资源管理器呢,explorer.exe进程的作用就是让我们管理计算机中的资源! 今天开电脑的时候就一直提示windows资源管 ...
- Build Your Hexo Blog (On Github)
超简单,比jekyll好多了! 看个Demo http://kevinjmh.github.io/ 了解Hexo Hexo是一个由Node.js驱动的,简单.快速.强大的Blog框架.可以快速的生成静 ...
- Javascript模式(三) 策略模式
var data = { "username" : "zhangsan", "password" : "12345690" ...
- HDOJ Oulipo 1686【KMP】
Oulipo Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- uva 1493 - Draw a Mess(并查集)
题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...
- Maven自动生成web.xml配置文件
没有这个文件会报错误的: 1. 2.在Maven下面设置这个: src/main/webapp OK生成了
- 介绍JSON
0x00 介绍JSON 介绍JSON :http://www.json.org/json-zh.html Introducing JSON :http://www.json.org/