- 浏览: 141194 次
-
文章分类
- 全部博客 (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 1183[分析] 暴力的办法就是从1开始检查每个数是否是丑数,发现丑数 ... -
Leetcode - Paint House II
2015-08-20 20:37 1626There are a row of n houses, ea ... -
Leetcode - Maximum Square
2015-08-16 13:33 537Given a 2D binary matrix filled ... -
Leetcode - Paint House
2015-08-16 10:48 1165There are a row of n houses, ea ... -
Leetcode - Different Ways to Add Parentheses
2015-07-29 20:21 1226Given a string of numbers and o ... -
Jump Game II
2015-07-05 16:49 571Given an array of non-negative ... -
Leetcode - Jump Game
2015-07-05 15:52 549Given an array of non-negative ... -
Leetcode - Interleaving String
2015-06-07 11:41 632Given s1, s2, s3, find whe ... -
Leetcode - Wildcard Matching
2015-06-06 20:01 1008Implement wildcard pattern ma ... -
Leetcode - Maximal Square
2015-06-04 08:25 637Given a 2D binary matrix fille ... -
Leetcode - Palindrome Partition II
2015-05-21 21:15 697Given a string s, partition ... -
Leetcode - Palindrome Partition
2015-05-21 09:56 805Given a string s, partition s s ... -
Leetcode - House Robber II
2015-05-20 22:34 791Note: This is an extension of ... -
Leetcode - Maximum Rectangle
2015-05-20 08:58 517Given a 2D binary matrix fill ... -
Leetcode - Scramble String
2015-05-17 14:22 604Given a string s1, we may repre ... -
Leetcode - Regular Expression Matching
2015-05-16 16:31 429Implement regular expression ma ... -
Leetcode - Best Time to Buy and Sell Stock IV
2015-05-01 16:11 633Say 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 501Say you have an array for whi ... -
Leetcode - Dungeon Game
2015-04-21 09:50 484The demons had captured the pr ...
相关推荐
在探讨js-leetcode题解之115-distinct-subsequences.js这一主题时,我们需要深入理解问题的背景、算法的核心原理,以及JavaScript语言在解决这个问题中的具体应用。 ### 问题背景 题目编号115,即“不同的子序列”...
在处理字符串匹配和子序列问题时,LeetCode上的题目“115. 不同的子序列”是一个经典的动态规划问题。给定两个字符串s和t,要求我们计算s中有多少个不同的子序列等于t。一个子序列可以不连续,但必须保持原有的顺序...
10. leetcode-115-Distinct-Subsequences.md:第115题,不同的子序列,涉及到动态规划和字符串处理。 这些题目覆盖了各种算法和数据结构,包括字符串处理、动态规划、树结构遍历、数组处理、位运算、深度优先搜索、...
题目“Distinct Subsequences”要求我们计算在给定字符串S中不同的子序列数量。 为了解决这个题,我们首先需要理解“子序列”的定义。在字符串理论中,子序列是由另一个序列删除一些元素(也可能不删除)且不改变...
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** (柱状图中...