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 = ...
随机推荐
- 对比Model前后数据保存不同值
1.示例代码 public class SysLogBLL<T,W> : BLLBase where T : new() { #region 比较方法 /// <summary> ...
- html文档加载顺序简单理解
html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- python模拟浏览器文件上传,csrf放行
服务器端视图函数 from django.shortcuts import render,HttpResponse from django.views.decorators.csrf import c ...
- jnhs-SpringMVC的controller向jsp传递数据的五种方式
参考此文http://blog.sina.com.cn/s/blog_6d3c1ec601014h1h.html 1 使用ModelAndVoew 引入:org.springframework.web ...
- poi操作word,简单写docx
参考博客: https://www.cnblogs.com/guilty/p/3977016.html 在HWPF中换行符是"\013",在XWPF中是run.addBreak() ...
- spring JdbcTemplate最基本的使用
package com.com.jdbctemplate; import org.springframework.jdbc.core.JdbcTemplate; import org.springfr ...
- Ubuntu 服务器默认的root账号是没有激活的,需要用初装的用户账号给root设置管理密码
user@ubuntu12:~$ sudo password root //用sudo修改账户 1.根据提示输入当前用户的密码: 2.修改成功之后你就可以使用root账号了,可以使用su root 命 ...
- 简单的layui二级联动
用layui实现省市二级联动, 需要注意的是使用layui之后, 你看到的下拉选框就不是option了,而是一些div 1.select表单 2.JS, ajax返回的是普通的数组
- 转:Android新特性介绍,ConstraintLayout完全解析
转:http://blog.csdn.net/guolin_blog/article/details/53122387 本篇文章的主题是ConstraintLayout.其实ConstraintLay ...
- Ubuntu下安装Mongo方法
场景:Ubuntu14下安装mongo,建议不要使用apt-get install 的命令安装,因为版本比较老 1.deb下载地址(可以自行选择OS,版本,server或tool或shell)http ...