40. Interleaving String
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.
思想: 动态规划。
DP[i][j] = (DP[i-1][j] && s1[i-1] == s3[i+j-1]) || (DP[i][j-1] && s2[j-1] == s3[i+j-1]); 其中,i, j 分别为字符串 s1, s2 的当前长度。
方法一:
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
if(s1.size() + s2.size() != s3.size()) return false;
if(s1 == "") return s2 == s3;
if(s2 == "") return s1 == s3;
vector<vector<bool> > DP(s1.size()+1, vector<bool>(s2.size()+1, false));
DP[0][0] = true;
for(size_t i = 1; i <= s1.size(); ++i) {
DP[i][0] = (DP[i-1][0] && s1[i-1] == s3[i-1]);
for(size_t j = 1; j <= s2.size(); ++j) {
DP[0][j] = (DP[0][j-1] && s2[j-1] == s3[j-1]);
DP[i][j] = (DP[i-1][j] && s1[i-1] == s3[i+j-1]) || (DP[i][j-1] && s2[j-1] == s3[i+j-1]);
}
}
return DP[s1.size()][s2.size()];
}
};
方法二: 进一步优化,空间复杂度, O(min(s1.size(), s2.size()));
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
if(s1.length() + s2.length() != s3.length()) return false;
if(s1 == "") return s2 == s3;
if(s2 == "") return s1 == s3;
if(s2.size() > s1.size()) s1.swap(s2); // make the sapce O(min(s1.size(), s2.size()));
vector<bool> DP(s2.size()+1);
for(size_t i = 0; i <= s1.size(); ++i) {
DP[0] = (!i || (DP[0] && s1[i-1] == s3[i-1]));
for(size_t j = 1; j <= s2.size(); ++j) {
DP[j] = (DP[j] && s1[i-1] == s3[i+j-1]) || (DP[j-1] && s2[j-1] == s3[i+j-1]);
}
}
return DP[s2.size()];
}
};
40. Interleaving String的更多相关文章
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- 【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 ...
- [LeetCode] Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- Interleaving String
https://leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is formed by th ...
随机推荐
- curl请求的时候总是提示400
今天用curl测试一个接口,一直提示400 最后发现是url的问题,如下处理就可以了 $url = str_replace(' ', '+', $url);
- enmo_day_07
数据备份 物理备份 : 底层数据块 逻辑备份 :exp(export), imp(import) 导入导出工具,提取成dump文件,再将dump文件放入数据库 expdp, impdp 数据蹦 uti ...
- Objective-C学习笔记-第一天(2)
Objective-C中的协议,相当于Java中的接口 参考:http://www.cnblogs.com/zzy0471/p/3894307.html 一个简单的协议遵循: PersonProtoc ...
- 常用加实用的Linux命令
命令是计算机执行任务的指令.可以使用命令去关闭计算机,或者列出当前目录的文件列表,或当前文本的内容,或者屏幕显示一条消息等. 下面是各种基本的命令可供参考. 1.Ls - List ls会列举出当前工 ...
- SpringMVC 用http请求的Get和Post请求作为路由的方法的重载方式
@Controller @RequestMapping("/messageProcessing") public class WechatPushController { @Aut ...
- LoadRunner常见问题
1.Error -27257: Pending web_reg_save_param/reg_find/create_html_param[_ex] request(s) detected and r ...
- (实用篇)PHP定时任务获取微信access_token
最近开发微信公众平台,公众号调用各接口时都需使用access_token,access_token是公众号的全局唯一接口调用凭据,开发时需要进行妥善保存. access_token有效期为7200秒 ...
- 或许是 Nginx 上配置 HTTP2 最实在的教程了
导读 从 2015 年 5 月 14 日 HTTP/2 协议正式版的发布到现在已经快有一年了,越来越多的网站部署了 HTTP2,HTTP2 的广泛应用带来了更好的浏览体验,只要是 Modern 浏览器 ...
- 移动互联网实战--资源类APP的数据存储处理和优化
前言: 对于资源类的APP, 其音频/图形占据了APP本身很大的比例. 如何存储和管理这些资源文件, 成了一个颇具挑战性的难点. 移动端的碎片化, 高中低端手机的并存, 需要开发者不光是具备基础的存储 ...
- Model1
jsp+javabean的开发模式 此处JavaBean也可是封装的业务逻辑 流程: 浏览器端访问jsp,jsp交给Javabean处理,javabean处理后台数据,交还给Jsp