LeetCode OJ-- Interleaving String **@
https://oj.leetcode.com/problems/interleaving-string/
刚开始用递归做,但是超时了
class Solution {
public:
bool flag;
bool isInterleave(string s1, string s2, string s3) {
flag = false;
if(s1.size() + s2.size() != s3.size())
return flag;
subIsInterleave(s1,s2,s3,,,);
return flag;
}
void subIsInterleave(string &s1, string &s2, string &s3, int p1, int p2, int p3)
{
if(p1 == s1.size() && p2 == s2.size() && p3 == s3.size())
{
flag = true;
return;
}
if(!(p1 <= s1.size() && p2 <= s2.size() && p3 < s3.size()))
return;
if(s3[p3] != s1[p1] && s3[p3] != s2[p2])
{
return;
}
if(p1 < s1.size() && s3[p3] == s1[p1] && flag == false)
subIsInterleave(s1,s2,s3,p1 + , p2,p3 + );
if(p2 < s2.size() && s3[p3] == s2[p2] && flag == false)
subIsInterleave(s1,s2,s3,p1,p2+,p3 + );
}
};
按照二维动态规划的思路,用两层for循环做
记 flag[i][j] 为 s1[0,i] s2[0,j] 匹配 s3[0,i+j] 则:
flag[i][j] = s1[i-1] == s3[i+j-1] && flag[i-1][j] || s2[j-1] == s3[i+j-1] && flag[i][j-1];
代码如下:
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
if(s3.size() != s1.size() + s2.size())
return false;
vector<vector<bool> > flag(s1.size()+, vector<bool>(s2.size()+, true));
// init
for(int i = ; i < s1.size() + ; i++)
flag[i][] = flag[i-][] && (s1[i-] == s3[i-]);
for(int j = ; j < s2.size() + ; j++)
flag[][j] = flag[][j-] && (s2[j-] == s3[j-]);
for(int i = ; i < s1.size() + ; i++)
for(int j = ; j < s2.size() + ; j++)
{
flag[i][j] = ((s1[i-] == s3[i+j-]) && flag[i-][j]) || ((s2[j-] == s3[i+j-]) && flag[i][j-]);
}
return flag[s1.size()][s2.size()];
}
};
LeetCode OJ-- Interleaving String **@的更多相关文章
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- [LeetCode] 97. Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1and s2. Example 1: Input: s1 = ...
- [Leetcode][JAVA] Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- 【leetcode】 Interleaving String (hard)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: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 ...
- Java for LeetCode 097 Interleaving String 【HARD】
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- Leetcode#97 Interleaving String
原题地址 转化为二维地图游走问题. 比如s1="abab",s2="aab",s3="aabaabb",则有如下地图,其中"^&q ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
随机推荐
- 笔记-python-standard library-8.1 data types-datetime
笔记-python-standard library-8.1 data types-datetime 1. Datatimes 本章节内容为日期和时间处理类和方法. 1.1. date ...
- Session只读的影响
.net中使用Session必须实现IRequiresSessionState接口,不过还有个只读的接口IReadOnlySessionState, 若是实现只读的接口,那么在该页面(如一般处理程序) ...
- sql中保留一位小数的百分比字符串拼接,替换函数,换行符使用
select num ,cast(round(convert(float,isnull((a.Sum_Num-d.Sum_Num),0))/convert(float,c.Sum_Store_Num ...
- BugKu-妹子的陌陌
打开后看这张图片,先放winhex里面,文件头FFD8,是jpg图片.看文件尾并不是FFD9,所以binwalk分析一下. 发现有一个rar文件,然后用foremost分离.发现里面有个加密的rar文 ...
- Python2 HTMLTestRunner自动化测试报告美化
python2 的测试报告美化,需要的同学直接用 #coding=utf-8 """ A TestRunner for use with the Python unit ...
- Java学习2
final在修饰类时,并不限制用户修改对象包含的变量值,只是限制了对象的主转移,只能针对某一个对象进行操作,中途不可更改对象. 重写父类的方法 重写(Override)和重载(Overload)都是针 ...
- Block Nested-Loop 和 Batched Key Access
官方文档:https://dev.mysql.com/doc/refman/5.7/en/bnl-bka-optimization.html BNL和BKA是MySQL 表关联的两种关联算法 比如t1 ...
- LAMP第二部分apache的配置
1. 下载discuz! mkdir /data/wwwcd /data/wwwmv /root/Discuz_X3.2_SC_GBK.zip .wget http://download.comsen ...
- java主要集合类的数据结构学习
http://www.cnblogs.com/beanmoon/archive/2012/11/22/2782442.html 在程序中,集合类每天都在使用,以致于某些代码充斥着List和Map,一直 ...
- BZOJ 1452:[JSOI2009]Count(二维树状数组)
[JSOI2009]Count 描述 输入 输出 1 2 分析: 裸二维bit,对每个颜色建一颗bit. program count; var bit:..,..,..]of longint; a:. ...