Leetcode#97 Interleaving String
转化为二维地图游走问题。
比如s1="abab",s2="aab",s3="aabaabb",则有如下地图,其中"^"表示开始位置,"$"表示终止位置。
a a b <- s2
s1 -> a ^ . .
b . . .
a . . .
b . . $
现在问题变成了,能否走出一条等于s3的路径?
对于地图上的任意一点,不妨设坐标为(i, j),令k为当前坐标下s3对应的位置。
如果s1[i]=s3[k]则可以向下走
如果s2[j]=s3[k]则可以向右走
否则意味着无路可走了
令res[i][j]表示s1[i..s1.length-1]和s2[j..s2.length-1]能否组成s3[k..s3.length],其中s3.length-k=(s1.length-i) + (s2.length-j)
则有递推公式:res[i][j] = (s1[i]等于s3[k],且 res[i+1][j]) 或 (s2[j]等于s3[k],且res[i][j+1])
由于计算res[i][j]时只用到了res[i+1][j]或res[i][j+1](相邻层),所以在具体编码实现的时候可以进行状态压缩,用一维数组保存res[i][j]
代码:
bool isInterleave(string s1, string s2, string s3) {
int len1 = s1.length();
int len2 = s2.length();
int len3 = s3.length();
vector<bool> res(len2 + , false); if (len1 + len2 != len3) return false;
if (!len1) return s2 == s3;
if (!len2) return s1 == s3; res[len2] = true;
for (int j = len2 - ; j >= ; j--)
res[j] = s3[len3 - (len2 - j)] == s2[j] && res[j + ]; for (int i = len1 - ; i >= ; i--) {
for (int j = len2 - ; j >= ; j--) {
int k = len3 - (len1 - i) - (len2 - j);
res[j] = (s3[k] == s1[i] && res[j]) || (s3[k] == s2[j] && res[j + ]);
}
} return res[];
}
Leetcode#97 Interleaving String的更多相关文章
- [LeetCode] 97. Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1and s2. Example 1: Input: s1 = ...
- leetcode 97 Interleaving String ----- java
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- [leetcode]97. Interleaving String能否构成交错字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Input: s1 = "aabc ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- 【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
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 97. Interleaving String
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...
- leetcode@ [97] Interleaving Strings
https://leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is formed by th ...
- [Leetcode][JAVA] Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
随机推荐
- HTML5+CSS3前端开发资源整合
HTML5+CSS3前端开发资源整合 推个广告 个人网站:http://www.51pansou.com HTML5视频下载:HTML5视频 HTML5源码下载:HTML5源码 meta相关: & ...
- leetcode刷题笔记
(1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...
- Hao123这个流氓
Author:KillerLegend Date:2014.2.27 From:http://www.cnblogs.com/killerlegend/p/3572591.html Hao123真让人 ...
- 实战Django:官方实例Part6
我们终于迎来了官方实例的最后一个Part.在这一节中,舍得要向大家介绍Django的静态文件管理. 现在,我们要往这个投票应用里面添加一个CSS样式表和一张图片. 一个完整的网页文件,除了html文档 ...
- webpack 学习笔记 02 快速入门
webpack 的目标 将依赖项分块,按需加载. 减少web app的初始加载时间. 使每一个静态集合都能够作为组件使用. 有能力集成第三方库,作为组件使用. 高度可配置化. 适用于大型项目. INS ...
- COUNT(*),count(1),COUNT(ALL expression),COUNT(DISTINCT expression) BY Group by
select column_2,count(column_2) as 'count(column_2)' ,count(column_1) as 'count(column_1)' ,count(*) ...
- C 函数可变参数
C 函数可变参数 C 语言中用 ... 表示可变参数,例如: void fun(int x ...) 头文件 cstdarg.h 中包含可变参数类型va_list和处理可变参数的三个宏: va_lis ...
- 通过java反射实现简单的关于MongoDB的对象关系映射(ORM).
通过阅读MongoDB 3.2.1的官方文档中关于java 编程发现最新的文档并没有实现对对象到Document的映射,所以自己有了利用反射实现简单的关系映射. 1.定义抽象类:AbstractMo ...
- acdream 1738 世风日下的哗啦啦族I
原题链接:http://acdream.info/problem?pid=1738 树套树裸题,如下: #include<algorithm> #include<iostream&g ...
- GNU make 总结 (三)
一.makefile 变量 makefile中的变量名是大小写敏感的,例如”foo”和”Foo”是两个不同的变量.通常情况下,对于一般变量,我们可以使用小写形式,而对于参数变量,采用全大写形式.当我们 ...