链接:
牛客链接:
https://www.nowcoder.com/practice/ed2923e49d3d495f8321aa46ade9f873?tpId=46&tqId=29065&tPage=1&rp=1&ru=/ta/leetcode&qru=/ta/leetcode/question-ranking
leetcode链接:
https://leetcode-cn.com/problems/distinct-subsequences/
描述和示例:

代码:
方法一:
class Solution {
public:
int numDistinct(string S, string T)
{
int row = S.size();
int col = T.size();
vector<vector<int>> arr(row + 1, vector<int>(col + 1));
arr[0][0] = 1;
for (int i = 1; i <= row; ++i)
{
arr[i][0] = 1;
for (int j = 1; j <= col; ++j)
{
if (S[i - 1] == T[j - 1])
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
else
arr[i][j] = arr[i - 1][j];
}
}
return arr[row][col];
}
};
方法一分析:
最终结果图:




方法二:
class Solution {
public:
int numDistinct(string S, string T)
{
int row = S.size();
int col = T.size();
vector<int> arr(col + 1);
arr[0] = 1;
for (int i = 1; i <= row; ++i)
{
for (int j = col; j > 0; --j)
{
if (S[i - 1] == T[j - 1])
arr[j] = arr[j - 1] + arr[j];
}
}
return arr[col];
}
};
方法二分析:
因为方法一中的二维数组我们只用到了一行数据
当遍历到一个点时,这个点的取值之和前一个有关:
当相同时:等于自身值加上前一个值
当不同时:等于自身值

3710

被折叠的 条评论
为什么被折叠?



