leetcode 115. Distinct Subsequences

【STM32】R05D电控红外协议的美的空调遥控器 目录一、设计思路一、R05D红外协议原理1.协议手册理解2.验证时序(重点)二、硬件实现1.需要的材料2.对发射模块电路进行修改3.STM32 GPIO选择三、代码实现1.载波38kHZ实现2.R05D时序实现3.调用函数并验证 一、设计思路 通常红外遥控采用NEC传输协议,而美的空调采用的是R05D红外协议(应该是自己设计的协议),因此用一般红外编码发射模块无法直接对空调进行控制。 解决方法:获取R05D协议手册 + 用红外接收管对原有遥控器红外接收进行波形分析。 一、R05D红外协议原理 R05D电控功 阅读详情

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 the characters without disturbing the relative positions of the remaining characters. (ie, “ACE” is a subsequence of “ABCDE” while “AEC” is not).

Example 1:

Input: S = “rabbbit”, T = “rabbit”
Output: 3
Explanation:

As shown below, there are 3 ways you can generate “rabbit” from S.
(The caret symbol ^ means the chosen letters)

rabbbit
^^^^ ^^
rabbbit
^^ ^^^^
rabbbit
^^^ ^^^

给出一个S字符串,一个T字符串,问S可以有多少种方法变成T,只允许删除S中的字符而不能改变顺序
如上例子,S = “rabbbit”, T = “rabbit”
可以通过删除S第一,第二,第三个“b“这3种方式来得到T

思路:
S中的每个字符都可以有保留和删除两种操作
保留第 j 位字符的时候是一种匹配,删除 j 位字符的时候是一种匹配,要把这两种情况加起来

用dynamic programming来解决,二维dp

这里用x轴用来表示S, dp[0][0]表示的是S和T都是空字符串,这时S和T完全匹配
所以dp[0][0] = 1

对于第0行的j > 0的所有列,表示T=“”,S不是空,这时候S只有一种方法转换成T,就是删除S的所有字符,所以第一行都是1

对于第1列的 i > 0的所有行,表示T不是空,但是S是空,所以S无法转换成T,所以都是0

y轴表示T

T S:  "" r a b b b i t ( j )
""     1 1 1 1 1 1 1 1
r      0
a      0
b      0
b      0
i      0
t      0
 ( i )

接下来是 i > 0 和 j > 0的部分
比如i = 1,j = 1, 这时候S=“r“, T=”r“, (为方便,i,j为1开始的index)
选择保留S[ j ]:就要看S和T的前一位有多少种方式,前一位是S,T都为空,所以看dp[0][0]
选择删除S[ j ]:就要看S的前一位和T有多少种方式,S前一位是空,T当前是"r",所以看dp[1][0]
这两种方式加起来是dp[0][0] + dp[1][0]
即S[j] == T[i]时:dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1]

S[j] != T[i]时,比如i = 1, j= 2, 即S=“ra”, T=“r” (为方便,i,j为1开始的index)
S[2] != T[1], 这时应该删除"a", 看S前一位和T是否匹配,即看S="r"和T=“r”
也就是dp[1][1]
即S[j] != T[i]时:dp[i][j] = dp[i][j - 1]

最后返回dp[s.length()][t.length()]

    public int numDistinct(String s, String t) {
        if (s.length() < t.length()) {
            return 0;
        }
        if (s == null || t == null) {
            return 1;
        }
        
        int[][] dp = new int[t.length() + 1][s.length() + 1];
        dp[0][0] = 1;
        
        //s="" and t.length>0, no match..
        for (int i = 1; i <= t.length(); i++) {
            dp[i][0] = 0;
        }
        
        //t="" and s.length>0, then only one way to get t is to delete all chars in s..
        for (int j = 1; j <= s.length(); j++) {
            dp[0][j] = 1;
        }
        
        for (int i = 1; i <= t.length(); i++) {
            for (int j = 1; j <= s.length(); j++) {
                if (s.charAt(j - 1) == t.charAt(i - 1)) {
                    //use s[j] or delete s[j]..
                    dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1];
                } else {
                    //delete s[j]..
                    dp[i][j] = dp[i][j - 1];
                }
            }
        }
        
        return dp[t.length()][s.length()];
    }
大数据利器Hadoop:从基础到实战,一篇文章掌握大数据处理精髓! 在当今大数据时代,数据量的爆炸式增长对企业和技术提出了前所未有的挑战。如何高效地存储、处理和分析这些庞大的数据集,成为了亟待解决的问题。Hadoop作为一种分布式计算框架,应运而生,为大数据处理提供了有效的解决方案。Hadoop是一个由Apache软件基金会维护的开源项目,它基于Google的分布式文件系统(Google File System,GFS)和MapReduce计算模型设计。Hadoop的主要目标是处理大规模数据集,它可以在普通硬件集群上运行,从而降低了大数据处理的成本。 阅读详情

相关推荐

GD32模拟IIC读写DVC1124

GD32模拟IIC读写集澈AFE芯片DVC1124

ponyma_的博客 1227

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...

不慌不忙、不急不躁 317

从模组到集装箱:手把手拆解一个储能BMS的软硬件架构设计与通信组网

本文深入解析储能BMS的软硬件架构设计与通信组网,从电芯采样到系统集成的全链条技术实现。详细介绍了三级硬件架构(AFE、CMU、BMU)的设计要点,以及软件分层实现中的驱动层、应用层核心算法和通信协议栈。通过实战案例分享CAN网络搭建、调试工具链和系统验证测试经验,为储能系统开发提供实用指导。

weixin_30752699的博客 465

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 ...

LeeWei 317

51单片机AHT20-21源代码

传感器输出经过 标定的数字信号,标准 I 2 C 格式。AHT20 配有一个全新设计的 ASIC专用芯片、一 个经过改进的MEMS半导体电容式湿度传感元件和一个标准的片上温度传感元件,其 性能已经大大提升甚至超出了前一代传感器的可靠性水平,新一代温湿度传感器, 经过改进使其在恶劣环境下的性能更稳定。但是现在网上的大多数原码都是针对stm32等的这种多位单片机,而我们都知道51单片机是8位单片机,无法对浮点数进行运算,对32位的整数计算有时候也不是很准确,所以我对网上stm32的例程进行了修改。...

wd0710的博客 4549

切比雪夫(最小区域法)直线拟合算法

切比雪夫,最小区域法直线拟合

闪电彬彬的博客 3965

LeetCode115—Distinct Subsequences

Distinct Subsequences

上善若水 581

DeepSeek Harness 本地部署与 AI 智能体开发全攻略

大语言模型(LLM)作为当前人工智能领域的核心技术,通过深度学习架构实现了对自然语言的深度理解与生成能力。其技术原理基于Transformer架构,通过海量文本数据训练,使模型能够捕捉语言的内在规律和语义关联。在工程实践中,LLM的价值在于能够作为智能体(Agent)的核心大脑,驱动各类应用实现自然语言交互、任务自动化和知识推理。常见的应用场景包括智能客服、代码生成、内容创作和数据分析等。本文聚焦于DeepSeek Harness这一开源AI应用开发框架,详细讲解如何从零开始部署这一智能体开发平台,并配置D

weixin_42527464的博客 378

Leetcode 115 Distinct Subsequences

思路一: 不容易啊,自己做出backtracking(dfs)的hard题。哈哈其实我是事先知道了这题要用dfs来做所以才直接往这方向靠的,不然的话也不一定我就能想到dfs。其实朝着dfs方向去想还是很容找到规律的。准确的说这边是使用了recursion and memorization,光用recursion会acception denied,因为time limit exceeded。下面简单说一下自己的思路吧:base case有两个,一个是当s长度小于t长度,返回0;另一个是当t的长度为1,只需简.

GoodJobJasper的博客 258

备战2025数学建模国赛(算法65):Fisher判别分析

在数学建模竞赛中,我们常常会遇到需要对研究对象进行分类或判断的问题。例如,根据西瓜的各类指标判断其好坏,根据经济数据判断一个国家的发展程度,或根据病人的医学影像特征判断其是否患病。这类“分类判别”问题,是建模竞赛中最常见的题型之一。Fisher判别分析(或称线性判别分析,LDA)作为一种经典的、有监督的降维与分类方法,因其模型简洁、可解释性强、计算高效而备受青睐。

YOLO 424

代码解读:使用Stable Diffusion完成相似图像生成任务

作为内容生产重要的一部分,生成相似图像是一项有意义的工作,例如很多内容创作分享平台单纯依赖用户贡献的图片已经不够了,最省力的方法就是利用已有的图片生成相似的图片作为补充。这篇博客详细解读基于Stable Diffusion生成相似图片的原理和代码。

沉迷单车的追风少年 1394

LeetCode //C - 115. Distinct Subsequences

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

Made in Code 1343

【DRAM存储器四】DRAM存储器的架构演进-part1

详述以提升吞吐量为目标的DRAM架构变化

highman110的博客 1254

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

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

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的博客 346

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

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的博客 404

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

【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 (ca...

lqy0927的博客 278

LeetCode115 Distinct Subsequences

详细见:leetcode.com/problems/distinct-subsequences Java Solution: github package leetcode; /* * Given a string S and a string T, count the number of distinct subsequences of * T in S. A s

zxwtry的博客 496

【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 (ca

指尖飞舞 3299

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的博客 364

leetcode-115. Distinct Subsequences

题目链接:点击打开链接 解: 代码C++: #include #include #include #include #include #include #include using namespace std; const int N = 10000; int table[N][N] = {0}; int numDistinct(string s, stri

surbeyond的专栏 346

[LeetCode] 115. Distinct Subsequences

[LeetCode] 115. Distinct SubsequencesGiven 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 str

ouwenjie10的专栏 282

【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

原题:Given a string S and a string T, count the number of distinct subsequences ofS which equals T.A subsequence of a string is a new string which is formed from the original string by deleting some (ca...

muche001的专栏 215

[leetcode] 115. Distinct Subsequences

Description 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 deleti...

Learning from the mistakes 243
上一篇: leetcode 97. Interleaving String
下一篇: leetcode 120. Triangle(三角形)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值