LeetCode97 Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. (Hard)
For example,
Given:
s1 = "aabcc"
,
s2 = "dbbca"
,
When s3 = "aadbbcbcac"
, return true.
When s3 = "aadbbbaccc"
, return false.
分析:
开始的思路是DFS搜索,当然结果也是华丽地超时了。
然后考虑动态规划。开始定义状态上出现了一些问题,甚至想到三维数组。
但是仔细考虑一下,采用双序列动态规划问题常见的状态定义,dp[i][j]表示s1的前 i 个字符和s2的前 j 个字符能否搭配组成s3(自然就是前i + j 位)。
然后就是递推关系式根据s1[i - 1] 与 s2[j - 1]与 s3[i + j - 1]是否相等(具体见代码)
注意:
s1,s2的长度加起来不等于s3的长度,直接返回false
初始化的时候发现二维数组直接用{false}并不能全部初始化,这里WA了一次
代码:
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
if (s1.size() + s2.size() != s3.size()) { //长度不一致判断
return false;
}
bool dp[s1.size() + ][s2.size() + ]; //初始化不能做到一次初始化问false dp[][] = true; for (int i = ; i <= s1.size(); ++i) {
if (s1[i - ] == s3[i - ] && dp[i - ][]) {
dp[i][] = true;
}
else {
dp[i][] = false;;
}
}
for (int i = ; i <= s2.size(); ++i) {
if (s2[i - ] == s3[i - ] && dp[][i - ]) {
dp[][i] = true;
}
else {
dp[][i] = false;
}
}
for (int i = ; i <= s1.size(); ++i) {
for (int j = ; j <= s2.size(); ++j) {
dp[i][j] = false;
if (s1[i - ] == s3[i + j - ] && s2[j - ] && s3[i + j - ]) {
dp[i][j] = (dp[i - ][j] || dp[i][j - ]);
}
else if (s1[i - ] == s3[i + j - ]) {
dp[i][j] = dp[i - ][j];
}
else if (s2[j - ] == s3[i + j - ]) {
dp[i][j] = dp[i][j - ];
}
}
}
return dp[s1.size()][s2.size()];
}
};
LeetCode97 Interleaving String的更多相关文章
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- 40. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 二维动态规划——Interleaving String
97. Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2 ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- LeetCode之“动态规划”:Interleaving String
题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- 【LeetCode】97. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- [Swift]LeetCode97. 交错字符串 | Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...
随机推荐
- centos7.6 安装jdk1.8
1. 下载 jdk-8u211-linux-x64.tar.gz文件. 2. 创建/opt/soft目录,# cd /opt, # mkdir soft, #tar -zxvf jdk-8u211- ...
- TZ_14_Zuul网关
1.spring-cloud的微服务大致是 2.zuul是 Netflix开源的微服务网关, 它可以和 Eureka. Ribbon. Hystrix等组件配合使用.zul的核心是一系列的过滤器,这些 ...
- SSM4-Linux上jdk、tomcat、zookeeper------tar zxvf的安装
1.zookeeper .Zookeeper介绍 官方推荐使用zookeeper注册中心. 注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发 ...
- HDU1950
//虽然是一道LIS问题,但是还是第一次用O(n*lgn)这种算法,赶角波错哈哈哈哈....至少今天有所收获 #include<cstdio> #include<cstring> ...
- C#里的应用程序域AppDomain
首先,描述一下AppDomain是什么:当一个程序集被执行时,系统就会自动为其创建一个AppDomain,每一个AppDomain属于某个进程,一个进程内可以有多个AppDomain:每个AppDom ...
- PHP实现微信申请退款流程实例源码
https://www.jb51.net/article/136476.htm 目录 前期准备: 前面讲了怎么实现微信支付,详见博文:PHP实现微信支付(jsapi支付)流程 和ThinkPHP中实 ...
- 微信小程序 this.setData() 详解
1.定义 setData()函数用于将逻辑层数据发送到视图层,同时对应的改变this.data的值. 2.setData()参数格式 接受一个对象,以键(key)值(value)的方式改变值. 其中, ...
- java httpclient中文乱码解决方案,看注释
@RequestMapping("getpage") public ModelAndView admin_checkurl(HttpServletRequest request) ...
- [jnhs]教训之jsp页面无法用jstl取值的坑.真他妈的奇葩,实体类的属性名不能用大写
结果页面永远都是空 调试发现,数据正常的塞进去了 问题解决: https://zhidao.baidu.com/question/570584436.html 实体类的属性名,首字母不能大写,改成小写 ...
- openpyxl 模块的使用
参考博客:https://www.cnblogs.com/anpengapple/p/6399304.html?utm_source=itdadao&utm_medium=referral 在 ...