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 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.
这道题是求字符串T是字符串S的字串的所有可能性的数目(不存在就是0)。
刚开始算的的时候比较暴力,所以超时了。
public class Solution {
char[] word1;
char[] word2;
int result = 0;
public int numDistinct(String s, String t) {
int len1 = s.length(),len2 = t.length();
if( len1 < len2 )
return 0;
word1 = s.toCharArray();
word2 = t.toCharArray();
for( int i = 0;i<=len1-len2;i++){
if( word1[i] == word2[0])
helper(i+1,1);
}
return result;
}
public void helper(int start1,int start2){
if( start2 == word2.length ){
result++;
return ;
}
for( int i = start1;i< word1.length ;i++){
if( word1[i] == word2[start2] )
helper(i+1,start2+1);
}
}
}
所以用DP算法。
DP,化归为二维地图的走法问题。
r a b b i t
1 0 0 0 0 0 0
r 1
a 1
b 1
b 1
b 1
i 1
t 1
如果当前字符相同,dp[i][j]结果等于用(dp[i-1][j-1])和(dp[i-1][j])求和
如果当前字符不同,dp[i][j] = dp[i-1][j]
public class Solution {
public int numDistinct(String s, String t) {
int len1 = s.length(),len2 = t.length();
if( len1 < len2 )
return 0;
char[] word1 = s.toCharArray();
char[] word2 = t.toCharArray();
int[][] dp = new int[len1+1][len2+1];
for( int i = 0;i<=len1;i++){
for( int j = 0;j<=i && j<=len2;j++){
if( i == 0 && j != 0)
dp[i][j] = 0;
else if( j == 0)
dp[i][j] = 1;
else if( word1[i-1] == word2[j-1] )
dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
else
dp[i][j] = dp[i-1][j];
}
}
return dp[len1][len2];
}
}
leetcode 115 Distinct Subsequences ----- java的更多相关文章
- 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 不同的子序列
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".构造如下地图("."表示空位 ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 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(hard)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- AJAX初步
1.什么是AJAX 客户端与服务器,可以在[不必刷新整个浏览器]的情况下,与服务器进行异步通讯的技术,即,AJAX是一个[局部刷新]的[异步]通讯技术: AJAX不是全新的语言,是2005年Googl ...
- C# Process打开程序并移动窗口到指定位置
process.start只是按指定的参数来运行一个程序,而这个程序自己运行起来是什么样子的就不是Process所能处理的了,不过当程序运行起来后倒是可以通过Process的MainWindowHan ...
- SPOJ COT2 树上找路径上不同值的个数
题目大意 给出多个询问u , v , 求出u-v路径上点权值不同的个数 开始做的是COT1,用主席树写过了,理解起来不难 很高兴的跑去做第二道,完全跟普通数组区间求k个不同有很大区别,完全没思路 膜拜 ...
- VS2010下配置OCI编程
OCI是Oracle官方出品的用于C/C++语言连接.操作Oracle数据库的API.在windows操作系统下使用VS等IDE编写.编译C++程序十分方便.简单,不需要使用Makefile.使用OC ...
- .net 小技巧
简单提示效果: <input runat="server" type="text" id="SelPerson" value=&quo ...
- JS中阻止默认事件与事件冒泡
JQuery 提供了两种方式来阻止事件冒泡. 方式一:event.stopPropagation(); $("#div1").mousedown(function(event){ ...
- metaprogramming笔记
动态多态与静态多态 动态多态:允许我们通过单个基类指针或引用处理多个派生类型的对象. 模板元编程中强调静态多态,允许不同类型的对象以同样的方式被操纵,只要它们支持某种共通的语法即可. 动态多态,连同& ...
- JS监听关闭浏览器事件
Onunload与Onbeforeunload Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过window.onunload来指定或 ...
- 黑马程序员——【Java基础】——多线程
---------- android培训.java培训.期待与您交流! ---------- 一.概述 (一)进程 正在执行中的程序,每一个进程执行都有一个执行顺序.该顺序是一个执行路径,或者叫一个控 ...
- 转载:socket.io 入门
原文链接:http://cnodejs.org/topic/50a1fcc7637ffa4155b5a264 我自己在用socket.io开发,对官方网站上的文档,进行简单的整理,然后自己写了一个简单 ...