[Leetcode] 115. Distinct Subsequences 解题报告

leetcode 115. Distinct Subsequences Hard | 115. 不同的子序列(动态规划) 题目 https://leetcode.com/problems/distinct-subsequences/ 题解 方法1:递归(超时) 这种解法比较容易理解,时间复杂度没算出来,但肯定不是 O(m*n)。提交之后超时了。 class Solution { public int numDistinct(String s, String t) { char[] ss = s.toCharArray(); char[] tt = t.toCharArray(); 阅读详情

题目:

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[i][j]表示S的前i个字符所包含的子字符串和T的前j个字符的匹配个数。此时可以分为两种情况:

1)s[i] != t[j],那么dp[i][j] = dp[i-1][j],即如果当前的字符串不相等,则dp[i][j]的值为s抛去第j-1个字符之后,所包含的子串匹配个数。

2)s[i] == t[j],那么dp[i][j]就可以继承dp[i-1][j-1]的值,再加上没有s[i-1]这个字符的值,即dp[i][j] = dp[i-1][j-1] + dp[i-1][j]。更简单的理解可以是:按照t[j-1]是和s[i-1]匹配还是和s[i-1]之前的字符匹配,可以将结果分为两类,即dp[i-1][j-1]和dp[i-1][j]。

另外初始状态dp[i][0] = 1,可以理解为空串是任意字符串的子串;而dp[0][j] = 0,可以理解为空串不能包含任意一个非空字符串。

代码:

class Solution {
public:
    int numDistinct(string s, string t) 
    {
        int s_size = s.length();
        int t_size = t.length();
        if(t_size > s_size || s_size == 0 || t_size == 0) {
            return 0;
        }
        vector<vector<int>> dp(s_size + 1, vector<int>(t_size + 1, 0));
        for(int i = 0; i <= s_size; ++i) {
            for(int j = 0; j <= t_size; ++j) {
                if (j == 0) {               // t is an empty string
                    dp[i][j] = 1;
                }
                else if (i == 0) {          // s is an empty string
                    dp[i][j] = 0;
                }
                else {
                    if(s[i-1] == t[j-1]) {
                        dp[i][j] += dp[i-1][j-1];
                    }
                    dp[i][j] += dp[i-1][j]; // discard the last character of s
                }
            }
        }
        return dp[s_size][t_size];
    }
};
PR工具timing report中setup time的计算过程 然后去pr工具里报告report_delay_calculation/reportDelayCalculation -from related_pin -to constraint_pin。这里constraint pin就是D pin (D0 D1 D2 D3),related pin就是 CK pin。我正在「拾陆楼」和朋友们讨论有趣的话题,你⼀起来吧? 阅读详情

相关推荐

uniCloud云存储进阶:七牛云扩展存储从开通、绑定域名到批量上传的完整实战

本文详细介绍了uniCloud云存储进阶实践,重点讲解如何通过七牛云扩展存储实现从开通、绑定域名到批量上传的完整流程。内容涵盖成本优势分析、域名备案方案、SSL证书配置、多文件并发上传控制等实战技巧,并提供可直接复用的代码示例,帮助开发者高效构建内容社区类应用。

weixin_26985923的博客 473

Leetcode 115 Distinct Subsequences 解题报告

Distinct Subsequences Total Accepted:38466Total Submissions:143567My Submissions QuestionSolution Given a stringSand a stringT, count the number of distinct subsequences ...

agn7047的博客 199

MuJoCo 机械臂关节空间阻抗控制Impedance实现(附代码)

阻抗控制通过调整关节的刚度和阻尼,让机器人在接触时产生顺应性,减少冲击力,其核心目标是在接触外部载荷时能够自适应调整输出力矩,实现力与位置的协同控制。阻抗控制可以很快的到达设定位置,在此基础上,使用mujoco交互中的施加外力操作,可以看到当外力松开后,机械臂也可以很快的回弹回设定位置。其中self.Kp和self.Kd为固定刚度和阻尼系数,通过修改刚度和阻尼参数,可以变得更柔顺来应对各种场景。注释掉该行后,直接simulate仿真可以看到机械臂控制位置震荡等情况,接下来引入关节空间阻抗控制的代码。

darcsdn 1284

[leetcode] 115. Distinct Subsequences 解题报告

题目链接:https://leetcode.com/problems/distinct-subsequences/ 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

小榕流光的专栏 726

西门子1200之PID用法与控制

西门子PID加热试验

gyxgyx789的博客 6284

NR RRC协议(2)---RRC连接建立流程

RRC连接建立目的是建立SRB1 RRC连接建立还用于将初始NAS消息从UE传送到网络 RRC连接的初始阶段期间,不激活AS安全性 RRC连接建立由UE侧发起

crg2046的博客 1960

[LeetCode 解题报告]115. Distinct Subsequences

Given a stringSand a stringT, count the number of distinct subsequences ofSwhich equalsT. A subsequence of a string is a new string which is formed from the original string by deleting some (ca...

CVer 134

【LeetCode】115. Distinct Subsequences 解题报告(Python)

这个题是要求不同子序列有几种组法,给定一个字符串 S 和一个字符串 T,计算在 S 的子序列中 T 出现的个数。 一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串。(例如,“ACE” 是 “ABCDE” 的一个子序列,而 “AEC” 不是) 示例 1: 输入:S = “rabbbit”, T = “rabbit” 输出:3 解释: 如下图所示, 有 3 种可以从 S 中得到 “rabbit” 的方案。 (上箭头符号 ^ 表示选取的字母) rabbbit ^

L141210113的专栏 336

基于STM32设计的智能井盖(Lora组网+4G上云)_423

设计并实现了一种基于 STM32 与 Lora 通信技术的智能井盖监测系统。系统采用 STM32F103C8T6 作为核心控制器,在井盖节点端集成水位检测、有害气体检测、倾斜角度检测以及 GPS 定位等功能模块,对井盖内部环境状态和姿态变化进行实时监测。各井盖节点通过 Lora 无线通信方式组成网络,将采集的数据发送至主机(网关)进行汇聚处理。

721

在 SAP S/4HANA Private Cloud 中把标准 CDS View 发布为 OData:可操作的四条路径与完整示例

在 ADT 中创建。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 734

Leetcode 115. Distinct Subsequences 计算子串 解题报告

1 解题思想给了S作为一个全串,然后试问,有多少种按顺序删除S中的某些元素而得到T的方案?其实这道题是个动态规划 以题目中的S = “rabbbit”, T = “rabbit” 为例子,可以看到上面的: 假设有两个指针I和j指向了T和S,DP矩阵表示已经匹配的数量 1、至少有,dp[i][j] = dp[i][j - 1]. 也就是说,即便在S[j] T[i]位置二者不相等,那么其也只好

MebiuW的专栏 1737

大模型微调部署实战及类GPT工具的高效使用

本文主要介绍了专栏《大模型微调部署实战及类GPT工具的高效使用》的核心内容,希望对使用大语言模型的同学们有所帮助。 文章目录 1. 前言 2. 专栏亮点 3. 你的收获 4. 详细目录

herosunly的博客 22万+

[leetcode115]Distinct Subsequences解题报告

115. Distinct Subsequences 代码即解题思路github地址 Given a string S and a string T, count the number of distinct subsequences of S which equals T. A subsequence of a string is a new string which is form...

初沏的茶的博客 430

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

LeetCode //C - 115. Distinct Subsequences

【代码】LeetCode //C - 115. Distinct Subsequences。

Made in Code 1344

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

Leetcode 115 Distinct Subsequences(DP)

题目连接:Leetcode 115 Distinct Subsequences解题思路:dp[i][j]表示s[0]~s[j]匹配前i个字符有多少种方式(s[j]一定匹配t[i]),dp[i][j] = sum(dp[i-1][k]) 其中k&lt;j,如果s[j] != t[i] 的话,dp[i][j] = 0。最后可以省略一层dp状态。class Solution { public: i...

不慌不忙、不急不躁 318

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

LeetCode 115. Distinct Subsequences

题目链接:不同的子序列 - 力扣 (LeetCode) Given a string S and a string T, count the number of distinct subsequences of S which equals T. A subsequence of a string is a new string which is formed from the original ...

zhaohaibo的博客 350

LeetCode(115) Distinct Subsequences

题目 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 (ca

指尖飞舞 3300

【Leetcode】115. Distinct Subsequences

位整数范围内,中间产生的答案仍然有可能超界的,比如。稍微聪明点的办法是,直接将每次的答案模。作为子序列出现了多少次。这道题需要注意一下,即使最终答案在。也非常长,但非常后面的地方有个。的值有可能会非常大以至于超出了。作为子序列,合起来就能拼成。时间复杂度一样,空间。

数学、算法爱好者的博客 262

LeetCode(115) Distinct Subsequences

题目如下: 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

BUILD 3858

LeetCode 115. Distinct Subsequences(动态规划)

Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of S which equals T. A subsequence of a string is a new string which is formed from the original string...

da_kao_la的博客 405

LeetCode 115 Distinct Subsequences 不同子串

LeetCode 115 Distinct Subsequences 不同字串 题目 Given a string S and a string T, count the number of distinct subsequences of S which equals T. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of

andyL_05的博客 366

【LEETCODE】115- Distinct Subsequences [Python]

题目: https://leetcode.com/problems/distinct-subsequences/ 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

aliceyangxi1987的博客 1002

Leetcode——115. Distinct Subsequences(C++)

Leetcode——115. Distinct Subsequences(C++)

begforgood的博客 389

Leetcode 115 Distinct Subsequences

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 non

无名山丘,崛起成峰 1309
上一篇: [Leetcode] 114. Flatten Binary Tree to Linked List 解题报告
下一篇: [Leetcode] 116. Populating Next Right Pointers in Each Node 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值