Leetcode 115 Distinct Subsequences

Unity与IOS交互,调用IOS系统相机和相册 与IOS的交互,在步骤上要比android简单许多,不会产生因为系统版本,或者包名导致的许多问题。过程中最头疼的一个问题是IOS回调Unity,很简单的一个问题,搞了半天。 阅读详情

在这里插入图片描述
思路一: 不容易啊,自己做出backtracking(dfs)的hard题。哈哈其实我是事先知道了这题要用dfs来做所以才直接往这方向靠的,不然的话也不一定我就能想到dfs。其实朝着dfs方向去想还是很容找到规律的。准确的说这边是使用了recursion and memorization,光用recursion会acception denied,因为time limit exceeded。下面简单说一下自己的思路吧:base case有两个,一个是当s长度小于t长度,返回0;另一个是当t的长度为1,只需简单计算此时s中有几个t就行,返回这个数字。为什么t长度会为1,因为在整个recursion过程中s,t的长度是一直在缩短的。说完了base case,我们来说normal case。我们遍历s,找到所有与t的第一个字符相等的字符,如果找到了,就执行numDistinc(s.substring(i+1),t.substring(1))。这样做的原因是:你想,我们已经在s的i位置找到了与t第一个字符相等的字符,那我们接下来要做什么呢?此时t的第一个字符已经匹配成功,那么我们是不是只要在(s从i之后的字符串)里找有多少个与(t从1之后的字符串)相等的字符串就行了,对吗。你看,这样recursion不就出现了吗,这就是为什么我们要计算numDistinc(s.substring(i+1),t.substring(1))的原因了。最后还有一个要注意的就是,每次recursion的结果我们都存进hashmap中,减少重复计算。思路还是很简单的,下面直接上代码:

class Solution {    
    private Map<String,Integer> mappings;
   
    private int res(String s, String t){

		// 这行代码其实并没有给mappings初始化,传入到numDistinc()的时候mappings = null
		// 这行代码可以删除,并不影响结果
        mappings = new HashMap<String,Integer>();
        
        return this.numDistinct(s,t);
    }
    
    public int numDistinct(String s, String t) {
        String key = s + " " + t;
        
		// 这行判断代码必须要有,因为一开始map是空的,map == null
		// 这时候containskey会报空指针的错
		// 我们只需要重新实例化一下就好
        if(mappings == null){mappings = new HashMap<String,Integer>();}
        
        if(mappings.containsKey(key)){return mappings.get(key);}
        if(s.length() < t.length()) {return 0;}
        int res = 0;
        for(int i = 0; i < s.length(); i++){
            if(s.charAt(i) == t.charAt(0)){
                if(t.length() == 1){
                    for(int j = 0; j < s.length(); j++){
                        if(s.charAt(j) == t.charAt(0)){
                            res++;
                        }
                    }
                    return res;
                }else{
                    String s1 = s.substring(i+1);
                    String t1 = t.substring(1);
                    res += numDistinct(s1,t1);
                }
            }
        }
        mappings.put(key,res);
        return res;
    }
}

思路二: 可以去lc的解答一看思路,虽然写的文章很长,但是耐心看下去真的很详细,解释的特别好,很有水平。他也是用recursion+memorization来实现的,只是在recursion上的想法和我不同。代码也很有水平,这边我直接上代码了:

class Solution {
    
    // Dictionary that we will use for memoization
    private HashMap<Pair<Integer, Integer>, Integer> memo;
    
    private int recurse(String s, String t, int i, int j) {
        
        int M = s.length();
        int N = t.length();
        
        // Base case
        if (i == M || j == N || M - i < N - j) {
            return j == t.length() ? 1 : 0;
        }
        
        Pair<Integer, Integer> key = new Pair<Integer, Integer>(i, j);
        
        // Check to see if the result for this recursive
        // call is already cached
        if (this.memo.containsKey(key)) {
            return this.memo.get(key);
        }
        
        // Always calculate this result since it's
        // required for both the cases
        int ans = this.recurse(s, t, i + 1, j);
        
        // If the characters match, then we make another
        // recursion call and add the result to "ans"
        if (s.charAt(i) == t.charAt(j)) {
            ans += this.recurse(s, t, i + 1, j + 1);
        }
        
        // Cache the result
        this.memo.put(key, ans);
        return ans;
    }
    
    public int numDistinct(String s, String t) {
        
        this.memo = new HashMap<Pair<Integer, Integer>, Integer>();        
        return this.recurse(s, t, 0, 0);
    }
}
非常清晰的思路以及代码,非常值得学习!

总结:

  1. 注意我自己方法里面的那个注释。报空指针的错原因是map = null。这边刚开始的时候map里面什么都没有,是空的,就是null,所以需要初始化一下。这边我还是有疑惑的,我觉得我已经在res()里面初始化过了,为什么map还是等于null呢。希望看完这篇文章有答案的同学能给我解答一下疑惑。Thanks in advance!
【S32K3 MCAL配置】-4.1-CAN Driver:如何解决CAN帧发送丢帧问题 阅读详情

相关推荐

基于STM32F407的DDS信号源设计与实现:DAC+DMA+TIM的高精度波形生成

本文详细介绍了基于STM32F407微控制器实现高精度DDS信号源的完整方案。通过结合DAC、DMA和TIM外设,并运用DDS算法,实现了分辨率高达0.001Hz的波形生成。文章深入剖析了相位累加器、波表查表等核心原理,提供了从CubeMX配置到代码实战的完整指南,并探讨了性能优化与高频输出问题的解决方案。

weixin_29193259的博客 503

Leetcode Golang 115. Distinct Subsequences.go

思路 dp dp[i][j]表示构成i长度的t,用到j长度的s,结果等于种类 转移方程: 如果t[i]==s[j],dp[i][j]=dp[i-1][j-1]+dp[i][j-1] 否则:dp[i][j]=dp[i][j-1] 初始值: dp[0][*]=1表示构成0长度的t,有一种方法 code func numDistinct(s string, t string) int { ls, lt...

anakinsun的博客 348

【数电课程设计】---出租车计价器

文章目录1、任务要求2、方案3、收获 1、任务要求 2、方案 3、收获

LZY 1万+

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

ESP32C6+ESP-IDF5.2实战:基于RS485的ModbusRTU从站开发与调试

本文详细介绍了基于ESP32C6和ESP-IDF5.2开发ModbusRTU从站的实战指南。涵盖硬件连接、协议实现、ESP-Modbus库移植、寄存器映射及调试技巧,帮助开发者快速构建稳定可靠的工业级RS485通信节点,并解决常见通信故障。

play7的博客 1188

eNSP无法启动AR报错码40,而且按照eNSP帮助手册排查都没用,我的处理方法【自己存档版】

eNSP无法启动AR报错码40,而且按照eNSP帮助手册排查都没用,我的处理方法【自己存档版】

mizhiakk的博客 8426

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

DQN网络代码调用env.reset()后存储观测信息报错问题ValueError: setting an array element with a sequence.解决处理

**reset()函数返回的是一个array类型以及其中数据的type!模拟游戏进行学习任务。因此需要将observation指定为真正需要的array信息即可,得到的的变量observation类型与Agent存储时的。因此怀疑返回的observation有问题,于是通过。报错信息提示为数据维度不对应,也即最初通过。在强化学习DQN网络代码实现过程中,针对。

Vulcan_Q的博客 4093

LeetCode115—Distinct Subsequences

Distinct Subsequences

上善若水 581

SAP Fiori Elements 扩展实战:UI 层扩展、OData 服务扩展、CDS 视图增强及全流程案例

UI 层扩展主要关注在不修改标准生成界面的基础上,自定义用户界面元素和交互逻辑。典型需求包括:在现有 Fiori Elements 界面上添加新的字段(如表格列或表单项)、增加自定义按钮并绑定操作、以及通过控制器扩展加入事件处理等。无论在 RAP 还是传统 BOPF 架构下,前端 UI 扩展通常采用 SAPUI5 提供的**扩展点(Extension Points)和控制器扩展(Controller Extension)**机制来安全地插入自定义代码。下面分别说明两种后端架构下 UI 扩展的实现方式。

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

【系统架构设计师-2023年真题】综合知识-答案及详解

要实现多任务间的协同工作,操作系统必须提供任务间的通信手段。嵌入式操作系统一般都会提供多任务间通信的方法,其中()是任务间最直接、最明显的通信方法,也是访问共享的数据结构,即不同的任务都可以访问同一地址空间。()作为一种更高级的通信方式,能够在同一处理器的各个任务间传递任意长度(理论上只受物理内存和机器字长限制)的信息。(1)A. 共享内存 B. Socket C. 消息传递 D. 信号量(2)A. 共享内存 B. Socket C. 消息传递 D. 信号量【答案】A、C关于星型拓扑结构,下列说法正确的是(

数据知道的博客 1万+

LeetCode //C - 115. Distinct Subsequences

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

Made in Code 1342

Statistics and Machine Learning Toolbox

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

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

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

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

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

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

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

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

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】

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

【Leetcode】115. Distinct Subsequences

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

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

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

指尖飞舞 3298

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

[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

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

xinxinxv的专栏 314
上一篇: Leetcode 294 Flip Game II
下一篇: Leetcode 187 Repeated DNA Sequences
GoodJobJasper
博客等级 码龄6年 0粉丝 278原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值