[LeetCode]97. Interleaving String

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

[LeetCode]97. Interleaving String

题目描述

这里写图片描述

思路

动态规划
以给出的样例为例

s1 = “aabcc”
s2 = “dbbca”
s3 = “aadbbcbcac”

dp01 a2 a3 b4 c5 c
0111000
1 d001100
2 b001110
3 b001011
4 c001010
5 a001011

dp数组的状态与他的前面的状态有关,前面的状态表示为上述表格中当前位置的左方或者上方,当前一位置为1时,表示到当前位置是满足的,可以生成当前的字符串,在下一位置时,就需要比较s3的对应位置是否和s1或者s2的对应位置相等

代码

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        if (s1.size() + s2.size() != s3.size())
            return false;
        vector<vector<int>> dp(s2.size() + 1, vector<int>(s1.size() + 1));
        for (int i = 0; i < s2.size() + 1; ++i) {
            for (int j = 0; j < s1.size() + 1; ++j) {
                if (i == 0 && j == 0)
                    dp[i][j] = 1;
                else if (i == 0 && j)
                    dp[i][j] = dp[i][j - 1] && s1[j - 1] == s3[i + j - 1];
                else if (i && j == 0)
                    dp[i][j] = dp[i - 1][j] && s2[i - 1] == s3[i + j - 1];
                else
                    dp[i][j] = (dp[i][j - 1] && s1[j - 1] == s3[i + j - 1]) || (dp[i - 1][j] && s2[i - 1] == s3[i + j - 1]);
            }
        }
        return dp[s2.size()][s1.size()];
    }
};

int main() {
    Solution s;
    cout << s.isInterleave("aabcc", "dbbca", "aadbbbaccc") << endl;

    system("pause");
    return 0;
}
STM32 USB2.0 转 Type-C 接口设计与实现 本文详细介绍了STM32 USB2.0转Type-C接口的设计与实现方案,涵盖硬件电路设计、软件配置及调试技巧。重点解析Type-C接口的CC引脚配置和电源管理,提供设备模式、主机模式及双角色模式的实现方法,帮助开发者解决实际应用中的连接稳定性和ESD防护等问题,提升产品可靠性和兼容性。 阅读详情

相关推荐

Paypal开发者中心获取“ClientId”和“ClientSecret”参数

正式环境设置 1、登录到 paypal开发者中心 网址:https://developer.paypal.com/developer/ 点击右上角登录按钮登录 2、左侧栏 - 我的应用程序和凭证 - 点击“创建新应用” 新注册的paypal账号,需要进行邮箱验证,验证通过后,需要等待一段时间(或重新登录),才会出现相关设置项位置 验证邮箱通过后,点击创建新应用 3、按提示填写,保存创建应用 4、点击刚创建的应用进入应用详情页 5、应用详情页获得网站开发人员所需要的“ClientId”和“Client

龚清林的博客 1万+

【Leetcode】97. Interleaving String(动态规划)(字符串组合)

Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2. Example 1: Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" Output: true Example 2: Input: s1 = "aabcc", s2 = "...

小I的博客 587

PFC中pb模型实战:从胶结参数设置到破坏模拟全流程解析

本文详细解析了PFC软件中平行胶结模型(PB模型)的实战应用全流程。从胶结参数(pb_kn, pb_ks, pb_ten等)的物理意义与精准赋值入手,深入探讨了模型构建、胶结施加以避免初始损伤的技巧,并系统介绍了破坏过程的监测、阈值判定方法以及高级可视化与常见问题诊断。旨在帮助岩土工程与颗粒材料研究者掌握从参数调试到破坏模拟的核心操作,提升数值模拟的准确性与效率。

php55的博客 1074

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

淡然坊 633

告别黑窗口:XShell+X11转发图形化界面的5分钟保姆级教程(附Xming配置)

本文详细介绍了如何使用XShell和X11转发技术快速实现Linux图形化界面的远程访问。通过5分钟快速配置指南,包括服务端设置、客户端工具选择及XShell+Xming的完美配置,帮助用户高效运行GUI应用程序,提升远程工作效率。特别适合需要图形化操作的开发者和运维人员。

weixin_29223603的博客 326

2021秋-北邮计算机导论期末考试(BUPT)

2021秋-北邮计算机导论期末考试(BUPT) 难度较低适合新手练习

weixin_73985779的博客 1497

leetcode【97】Interleaving String

问题描述 Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2. 题目意思就是给定三个字符串s1,s2和s3,判断s3能否由s1,s2交叉组成。 思路分析 用一个动态规划的方法,顶一个二维的bool类型的数组dp,dp[i][j]表示s3的前i+j个字符能否由s1的前i个字符和s2的前j...

fanyuwgy的博客 219

【一文读懂】NTN(非地面网络)技术介绍

NTN,即非地面网络,是指利用卫星、高空平台(如无人机、平流层气球等)等非地面通信基础设施来实现全球通信覆盖的网络。在5G时代,NTN主要基于5G标准,特别是5G NR(New Radio)技术,通过卫星通信扩展地面网络的覆盖范围和服务能力。NTN技术的出现和发展,为全球通信带来了新的可能性和挑战。通过不断的技术创新和标准化推进,NTN有望在未来发挥更加重要的作用,为全球用户提供无处不在的通信服务。随着6G时代的到来,我们有理由相信NTN技术将迎来更加广阔的发展前景。

u011376987的博客 1万+

97. Interleaving String

题目描述(困难难度) 在两个字符串 s1 和 s2 中依次取字母,问是否可以组成 S3。什么意思呢?比如 s1 = abc , s2 = de,s3 = abdce。 s1 取 1 个 字母得到 a,s1 再取个字母得到 ab,s2 取个字母得到 abd, s1 取 1 个 字母得到 abdc, s2 取 1 个 字母得到 abdce,然后就得到了 s3,所以返回 true。 解法一 回溯法 如果我们简化下问题,如果 s1 和 s2 中不含有重复的字母,比如 s1 = abc,s2 = de,s3 = a

猫敷雪 334

Ascend开发包Acllite安装

设置环境变量,配置程序编译依赖的头文件,库文件路径。“$HOME/Ascend”请替换“Ascend-cann-toolkit”包的实际安装路径。将YOLOV7原始模型转换为适配昇腾310处理器的离线模型(*.om文件),放在model路径下。运行完成后,会在样例工程的out目录下生成推理后的图片,显示对比结果如下所示。可以使用以下两种方式下载,请选择其中一种进行源码准备。请从以下链接获取该样例的输入图片,放在data目录下。执行以下命令,执行编译脚本,开始样例编译。执行运行脚本,开始样例运行。

如果想成为中心,那么就到中心去吧。 317

LSTM+Transformer混合模型时间序列预测实战教学

LSTM+Transformer混合模型时间序列预测实战教学 毕业设计

8218

97. Interleaving String(交错字符串)

题目链接:https://leetcode.com/problems/interleaving-string/ 思路: 一种思路用回溯递归来做,提交成功了,但是时间耗费693ms...... 另一种解法用DP: 【动态规划】 假设dp[i][j]表示s1的前i个字符和s2的前j个字符是否能够交错构成s3的前i+j个字符。 状态转移方程: dp[i][j]=(dp[i-1][j] &a...

God_Mood的博客 219

【100%通过率】华为OD机试真题 JavaScript【数字最低位排序】【最低位排序】

2023华为OD机试真题 JavaScript【数字最低位排序】【最低位排序】 给定一个非空数组(列表) 起元素数据类型为整型 请按照数组元素十进制最低位从小到大进行排序 十进制最低位相同的元素,相对位置保持不变 当数组元素为负值时,十进制最低为等同于去除符号位后对应十进制值最低位

MISAYAONE的博客 587

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

Devexpress16.2.6破解工具

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

温度PID控制PLC程序

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

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

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

LeetCode97—Interleaving String

LeetCode97—Interleaving String

上善若水 813

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

LeetCode 97. Interleaving String

97. Interleaving StringGiven 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 = “aa

mingjc的博客 399

Leetcode 97. Interleaving String

Leetcode 97. Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = &amp;quot;aabcc&amp;quot;, s2 = &amp;quot;dbbca&amp;quot;, s3 = &amp;quot;aadbbcbcac&amp;quot; Out

wyxwyx469410930的博客 405

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 = "aadbbba

xclfafa的博客 384

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

无名山丘,崛起成峰 1万+

[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

追梦船的专栏 583

LeetCode 97. Interleaving String 题解

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 = "aad

Guo15331092的博客 460

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

SmartSi 2387

【Leetcode】97. Interleaving String

题目地址: https://leetcode.com/problems/interleaving-string/ 给定三个字符串s1s_1s1​,s2s_2s2​和s3s_3s3​,则s1s_1s1​和s2s_2s2​可以通过交错的方式生成一个新字符串s′s's′,”交错的方式“的含义是,可以以插空的方式,将s1s_1s1​中的字符插到s2s_2s2​的字符之间,生成一个新的字符串,但s1s_1s1​的各个字符在新字符串中的相对顺序不能变。问s3s_3s3​能否由s1s_1s1​和s2s_2s2​这样交错生

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

LeetCode 97(Interleaving String)Java

原题:Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. 给定3个字符串,判断s3是否可以由s1和s2交错组成; 例如:s1="aa",s2="bc",s3="abca";s3可以由s1和s2交错组成; 思路:很多字符串问题某一位置的解会与它上一位置的解有关,这种情

Jassy1994的博客 581

leetcode 97 Interleaving String(python)

python leetcode 97 Interleaving String

快递小可的博客 1334
上一篇: [LeetCode]557. Reverse Words in a String III
下一篇: [LeetCode]221. Maximal Square
charon____
博客等级 码龄11年 3粉丝 202原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值