LeetCode Interleaving String

图解LeetCode97:Interleaving String 文章目录理解题意解决方法一:最简单解法解决方法二:动态规划代码实现总结 Leetcode算法系列将详细讲解一些经典的面试算法题。 今天的题目是个人非常喜欢的一个,题目的名字叫做“Interleaving String”,这个题目是什么意思呢?   理解题意 Interleaving 从字面上讲是交错交叉的意思,给定两个字符串s1:"a"和s2:“b”,那么对于s3:"ab"我们说s3是s... 阅读详情

原题链接在这里:https://leetcode.com/problems/interleaving-string/

这是一道DP题,用boolean数组dp维护历史数据,dp[i][j]表示长度为i的s1和长度为j的s2能否组成长度为 i+j 的s3.

递推式是若当前s3的最后一个char与s1的最后一个char相同,并且dp[i-1][j]为true, 那么dp[i][j]为true;

或者当前s3的最后一个char与s2的最后一个char相同,并且dp[i][j-1]为true, 那么dp[i][j]为true, 两者是 或 的关系。

初始化第一排时就是s2长度为0时,看s3和s1最后一个char若相同 并且 s1的前一个字符对应的dp是true, 那么返回就是true; 第一列同理。

Note: 1. dp的大小是 (len1+1) * (len2+1). dp[0][0] = true是s1,s2,s3长度都为0的情况。

2. for loop时 i 是 <= 而不是< len1.

3. 初始化第一行第一列时别忘了校验前一个dp的值, 如dp[i-][0].

4. 本题可以使用降维来做。

AC Java:

public class Solution {
    public boolean isInterleave(String s1, String s2, String s3) {
        if(s3.length() != s1.length()+s2.length()){
            return false;
        }
        int len1 = s1.length();
        int len2 = s2.length();
        boolean [][] dp = new boolean[len1+1][len2+1];
        dp[0][0] = true;
        for(int i = 1; i<=len1; i++){   //error
            if(s1.charAt(i-1) == s3.charAt(i-1) && dp[i-1][0]){ //error
                dp[i][0] = true;
            }
        }
        for(int j = 1; j<=len2; j++){
            if(s2.charAt(j-1) == s3.charAt(j-1) && dp[0][j-1]){
                dp[0][j] = true;
            }
        }
        
        for(int i = 1; i<=len1; i++){
            for(int j =1; j<=len2; j++){
                char c = s3.charAt(i+j-1);
                if(c == s1.charAt(i-1) && dp[i-1][j]){
                    dp[i][j] = true;
                }
                if(c == s2.charAt(j-1) && dp[i][j-1]){
                    dp[i][j] = true;
                }
            }
        }
        return dp[len1][len2];
    }
}


RK3588开发笔记-mipi csi接口imx415摄像头调试记录 摘要:本文详细介绍了RK3588开发板适配SONY IMX415 MIPI CSI摄像头的调试过程。从硬件环境准备(包括RK3588核心板、IMX415模块连接及测试工具)、软件环境配置(Linux 5.10内核、驱动状态确认),到设备树关键配置(MIPI链路、GPIO引脚、电源域等)都进行了详细说明。调试过程中通过dmesg日志、media-ctl拓扑分析等工具验证了摄像头驱动加载和图像采集功能。最终实现了800万像素高清图像采集,总结了硬件连接验证、信号完整性优化等关键经验,为类似MIPI摄像头调试提供 阅读详情

相关推荐

Cygwin安装教程

简介 cygwin是一个在windows平台上运行的unix模拟环境,是cygnus solutions公司开发的自由软件 Cygwin就是一个windows软件,该软件就是在windows上仿真linux操作系统 简言之,cygwin是一个在windows平台上运行的 linux模拟环境,使用一个Dll(动态链接库)来实现 这样,我们可以开发出Cygwin下的UNIX工具,使用这个DLL运行在W...

Errol's Blog 13万+

LeetCode算法系列:97.Interleaving String

题目描述: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" Output: true Example 2: Input: s1 = "aabcc", ...

Efan_W的博客 324

STM32新手必看:3分钟搞定ST官网资料下载(附F0系列手册+外设库下载技巧)

本文为STM32开发新手提供高效获取ST官网技术资料的完整指南,重点介绍如何快速下载STM32F0系列数据手册和外设库。通过系统性的资源分类解析和实用下载技巧,帮助开发者快速定位所需文档,建立本地知识库,提升开发效率。

ujm5678901的博客 268

Leetcode: Interleaving String

Problem: Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", ret

popfish的算法路 1万+

EEGNet 网络代码解析与实战复现指南

本文详细解析了EEGNet这一轻量级卷积神经网络的结构与设计思想,重点阐述了其利用深度可分离卷积高效处理脑电信号的原理。文章提供了基于PyTorch的完整代码复现指南,包括网络搭建、数据模拟、训练调试及关键参数解析,旨在帮助读者掌握EEGNet的代码复现技巧,并将其应用于脑机接口等实际场景。

honey的博客 532

Matlab编码转换实战:从GBK到UTF-8的完整解决方案

本文提供Matlab编码转换的完整实战方案,重点解决GBK与UTF-8编码冲突导致的中文乱码问题。通过修改系统配置文件、批量转换项目文件及设置默认保存编码,实现从GBK到UTF-8的彻底迁移,并分享常见避坑指南与自动化脚本技巧,确保代码跨平台协作的稳定性。

weixin_29031057的博客 323

leetCode 97.Interleaving String (交错字符串) 解题思路和方法

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", ret

Android进阶 2380

CANoe制作网关实现CAN(FD)报文故障注入(报文长度/timeout/信号错误/E2E)2

CANoe制作网关实现CAN报文故障注入(报文长度/timeout/信号错误/E2E) 2.新建CANoe工程 3.透传功能 4.故障注入panel面板 5.CAPL代码 5.1故障注入开始、停止、定时 5.2DLC故障注入 5.3 Timeout故障注入 5.4 信号故障注入

天赐好车的博客 3674

LeetCode:Interleaving String

Interleaving String Total Accepted: 49969 Total Submissions: 220816 Difficulty: Hard Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For exampl

walker lee 3278

把云端打印带回办公室:通信场景 SAP_COM_0466 打通 SAP BTP ABAP environment 与本地打印机的 Pull Integration 实战

摘要: 本文探讨云端SAP系统如何通过SAP_COM_0466通信场景实现与本地打印机的安全连接。核心方案采用Pull Integration模式,由本地组件(如SAP Cloud Print Manager或第三方OMS)主动从云端打印队列拉取任务,避免网络边界冲突。配置流程涵盖通信安排创建、打印队列绑定及权限管理,并强调队列类型必须选择"Pull Integration Queue"的关键细节。方案通过分离云端生产与本地打印环节,既保障安全性又便于运维监控,适用于SAP BTP AB

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

YOLO11涨点优化:训练技巧 | 使用标签平滑(Label Smoothing)配合余弦退火学习率,防止过拟合,稳步提点

摘要: YOLO11凭借动态卷积核调度、自适应特征融合等创新成为2026年目标检测主流,但面临训练集过拟合问题。文章聚焦两种零推理开销的正则化技巧:标签平滑(Label Smoothing)和余弦退火学习率(Cosine Annealing LR)。标签平滑通过软化硬标签(如将[1,0]改为[0.9,0.1])防止模型过度自信,代码中通过label_smoothing=0.1一键启用;余弦退火则以平滑的余弦曲线调整学习率(cos_lr=True),避免阶梯衰减的震荡。两者组合可显著缩小YOLO11训练与验证

2601_95807009的博客 45

【LeetCode】Interleaving String 解题报告

【题目】 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbb

跳出温水的青蛙 4061

把冷门数据从内存里请出去:ABAP 环境下玩转 SAP HANA Native Storage Extension (NSE) 的开发者实战笔记

摘要:SAP HANA NSE技术为ABAP系统提供了一种经济高效的温数据管理方案。文章首先指出企业数据存在"温度分层"现象,高频访问的热数据需常驻内存,而低频温数据更适合存储在磁盘或闪存。NSE通过混合列存架构,将不常访问的数据移至磁盘但仍保持数据库内统一管理,显著降低内存成本。 关键实施要点包括:在ADT技术设置中将Load Unit设为"Page Enforced"以启用NSE;理解buffer cache作为数据"抽屉"的工作原理;掌握选表

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

leetcode之Interleaving String

转自:http://blog.unieagle.net/2012/09/29/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Ainterleaving-string%EF%BC%8C%E4%BA%8C%E7%BB%B4%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92/ LeetCode题目:Interleaving String,二维

daisyZH的专栏 1000

jre9下载 jdk9

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

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Leetcode Interleaving String

题目和算法描述见http://yufeizhaome.wordpress.com/2014/08/19/leetcode-interleaving-string/ 递归的算法在两个string相同元素

thestoryofsnow的专栏 487

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Leetcode-97 Interleaving String

Leetcode-97 Interleaving StringquestionGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example, Given: s1 = “aabcc”, s2 = “dbbca”,When s3 = “aadbbcbcac”, return true

zhangwj0101的博客 297

efm32 入门.pdf

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

LeetCode力扣之97. Interleaving String

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", return false.packag...

LeeWei 344

【LeetCode】Interleaving String

题目描述: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbb

ice4026的专栏 353

Interleaving String - LeetCode

Interleaving String - LeetCode 题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = “aabcc”, s2 = “dbbca”, When s3 = “aadbbcb

SGAFPZ的博客 411

[LeetCode] Interleaving String

[Problem] Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", ret

1.02^365的成长裂变 3640

LeetCode97—Interleaving String

LeetCode97—Interleaving String

上善若水 813

【leetCode】Interleaving String

题意:给定3个字符串s1,s2,s3,判断s3是否由s1和s2插入而成 思路:分我的思路和网上的思路,其实本质是一样的。 我的思路:用old1,old2数组表示上一个s3的字符能用s1和s2的哪些字符组成,new1,new2表示当前s3字母能由s1和s2组成,对于old数组的点对old1[i],old2[j],若s2[j+1]==s3[k],则在new的数组里push(i,j+1),同理若s1

XiaYiZhanQiHang的博客 345

[LeetCode]Interleaving String

QuestionGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example, Given: s1 = "aabcc", s2 = "dbbca",When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", retu

Tomcat的专栏 359

leetcode Interleaving String

题目Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example, Given: s1 = “aabcc”, s2 = “dbbca”,When s3 = “aadbbcbcac”, return true. When s3 = “aadbbbaccc”, return fal

不积跬步,无以至千里 1678
上一篇: LeetCode Distinct Subsequences
下一篇: LeetCode Compare Version Numbers
Dylan_Java_NYC
博客等级 码龄11年 1粉丝 134原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值