- 浏览: 137638 次
文章分类
- 全部博客 (189)
- Tree (14)
- Dynamic Programming (34)
- Array (20)
- Search (1)
- Hash (12)
- Backtracking (22)
- Divide and Conque (8)
- Greedy (6)
- Stack (12)
- software (0)
- List (7)
- Math (22)
- Two pointers (16)
- String (20)
- Linux (1)
- Sliding Window (4)
- Finite State Machine (1)
- Breadth-first Search (7)
- Graph (4)
- DFS (6)
- BFS (3)
- Sort (9)
- 基础概念 (2)
- 沟通表达 (0)
- Heap (2)
- Binary Search (15)
- 小结 (1)
- Bit Manipulation (8)
- Union Find (4)
- Topological Sort (1)
- PriorityQueue (1)
- Design Pattern (1)
- Design (1)
- Iterator (1)
- Queue (1)
最新评论
-
likesky3:
看了数据结构书得知并不是迭代和递归的区别,yb君的写法的效果是 ...
Leetcode - Graph Valid Tree -
likesky3:
迭代和递归的区别吧~
Leetcode - Graph Valid Tree -
qb_2008:
还有一种find写法:int find(int p) { i ...
Leetcode - Graph Valid Tree -
qb_2008:
要看懂这些技巧的代码确实比较困难。我是这么看懂的:1. 明白这 ...
Leetcode - Single Num II -
qb_2008:
public int singleNumber2(int[] ...
Leetcode - Single Num II
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.
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.
public class Solution { public int numDistinct(String s, String t) { if (s == null) return 0; if (t == null || t.length() == 0) return 1; if (s.length() == 0) return 0; int lenS = s.length(); int lenT = t.length(); int[][] dp = new int[2][lenT + 1]; dp[0][0] = 1; int lastLenS = 0; int currLenS = 1; for (int i = 1; i <= lenS; i++) { currLenS = 1 - lastLenS; dp[currLenS][0] = 1; for (int j = 1; j <= lenT; j++) { dp[currLenS][j] = dp[lastLenS][j]; if (s.charAt(i - 1) == t.charAt(j - 1)) dp[currLenS][j] += dp[lastLenS][j-1]; } lastLenS = currLenS; } return dp[currLenS][lenT]; } // dp[i][j] = dp[i - 1][j] + (s[i - 1] == t[j - 1] ? dp[i - 1][j - 1] : 0); // dp[i][j]表示s的前i字符构成的子串能匹配t中前j个字符构成的子串的子序列数 // 不管s和t当前考察位置处的字符是否相等,dp[i][j]至少有dp[i-1][j] // 如果当前考察位置处两字符相等,则再加上dp[i-1][j-1]的数目 public int numDistinct1(String s, String t) { if (s == null) return 0; if (t == null || t.length() == 0) return 1; if (s.length() == 0) return 0; int lenS = s.length(); int lenT = t.length(); int[][] dp = new int[lenS + 1][lenT + 1]; dp[0][0] = 1; for (int i = 1; i <= lenS; i++) { dp[i][0] = 1; for (int j = 1; j <= lenT; 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[lenS][lenT]; } }
发表评论
-
Leetcode - Ugly Number II
2015-08-24 22:54 1163[分析] 暴力的办法就是从1开始检查每个数是否是丑数,发现丑数 ... -
Leetcode - Paint House II
2015-08-20 20:37 1603There are a row of n houses, ea ... -
Leetcode - Maximum Square
2015-08-16 13:33 507Given a 2D binary matrix filled ... -
Leetcode - Paint House
2015-08-16 10:48 1150There are a row of n houses, ea ... -
Leetcode - Different Ways to Add Parentheses
2015-07-29 20:21 1200Given a string of numbers and o ... -
Jump Game II
2015-07-05 16:49 546Given an array of non-negative ... -
Leetcode - Jump Game
2015-07-05 15:52 533Given an array of non-negative ... -
Leetcode - Interleaving String
2015-06-07 11:41 613Given s1, s2, s3, find whe ... -
Leetcode - Wildcard Matching
2015-06-06 20:01 997Implement wildcard pattern ma ... -
Leetcode - Maximal Square
2015-06-04 08:25 619Given a 2D binary matrix fille ... -
Leetcode - Palindrome Partition II
2015-05-21 21:15 684Given a string s, partition ... -
Leetcode - Palindrome Partition
2015-05-21 09:56 786Given a string s, partition s s ... -
Leetcode - House Robber II
2015-05-20 22:34 772Note: This is an extension of ... -
Leetcode - Maximum Rectangle
2015-05-20 08:58 502Given a 2D binary matrix fill ... -
Leetcode - Scramble String
2015-05-17 14:22 583Given a string s1, we may repre ... -
Leetcode - Regular Expression Matching
2015-05-16 16:31 416Implement regular expression ma ... -
Leetcode - Best Time to Buy and Sell Stock IV
2015-05-01 16:11 615Say you have an array for which ... -
Leetcode - Best Time to Buy and Sell Stock IV
2015-04-23 09:59 0public class Solution ... -
Leetcode - Best Time to Buy and Sell Stock III
2015-04-23 09:04 483Say you have an array for whi ... -
Leetcode - Dungeon Game
2015-04-21 09:50 459The demons had captured the pr ...
相关推荐
javascript js_leetcode题解之115-distinct-subsequences.js
python python_leetcode题解之115_Distinct_Subsequences
java java_leetcode题解之Distinct Subsequences.java
10. leetcode-115-Distinct-Subsequences.md:第115题,不同的子序列,涉及到动态规划和字符串处理。 这些题目覆盖了各种算法和数据结构,包括字符串处理、动态规划、树结构遍历、数组处理、位运算、深度优先搜索、...
gas station leetcode 在牛客网上的在线编程中的leetcode在线编程题解 代码中有详细题解 完成: 树 Minimum Depth ...distinct-subsequences 递归 valid-palindrome 模拟 pascals-triangle 模拟 pasca
Subsequences](./leetcode/动态规划-Distinct Subsequences.java) [动态规划-Longest Valid Parentheses](./leetcode/动态规划-Longest Valid Parentheses.java) [动态规划-Maximum Length of Repeated Subarray](./...
115.Distinct_Subsequences不同的子序列【LeetCode单题讲解系列】
leetcode 分类 LeetCode Progress 128/154 Other Solutions ...Subsequences Decode Ways Palindrome Partitioning II Maximal Rectangle ###Recursion N-Queens N-Queens II Balanced Binary Tree Binar
leetcode卡 OJ 题目汇总 记录下自己做过的oj题目, 也记录下自己对算法以及编程的理解与...Subsequences两种解法解题失败 [] 2014-06-14-13:00 ~ 2014-06-14-14:40 Interleaving String动态规划解决, Recovering BST失败
java lru leetcode ##Thinking in Java chapter21 ##Netty in Action ####chapter2: ...Subsequences] () [Rotate Image] () [Ugly Number] () [Ugly Number II] () [Repeated DNA Sequences] () [Lar
leetcode 【演示记录】 报告 展示 2017/03/06 1.二和,167.二和二 2107/03/06 15.3 总和,16.3 总和最近,18.4 总和,11.最多水的容器 2017/03/09 62.Unique Paths, 63.Unique Paths II, 64.Minimum Path Sum 2017/...
Distinct Subsequences、152. Maximum Product Subarray等。这类题目通常要求读者使用动态规划来解决问题,例如使用DP数组来记录状态、使用递归来计算结果等。 6. 排序和查找类题目 排序和查找类题目是LeetCode原...
10. **Distinct Subsequences** (不同的子序列) 统计一个字符串所有非空子序列在另一个字符串中出现的次数。动态规划方法,考虑当前字符是否出现在子序列中。 11. **Largest Rectangle in Histogram** (柱状图中...