【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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.
(二)解题
题目大意:给定三个字符串s1,s2,s3,判断s3是不是由s1和s2中的字符交叉存取组成,注意:在新字符串s3中应当保留原字符在s1和s2中的相对位置。
解题思想:采用动态规划的思想,每次将s1或s2中的字符与s3进行对比,分别有以下两种情况:
1、s1[i]==s3[k],此时i++,k++,继续进行比较
2、s2[j]==s3[k],此时j++,k++,然后继续比较
具体解释见代码注释:
class Solution {
public:
bool isfind;//记录是否为Interleaving String
bool isInterleave(string s1, string s2, string s3) {
if (s3.size() != s1.size() + s2.size()) return false;//大小必须先满足条件
isfind = false;//初始化isfind
vector<vector<int>> isSearch;//用来记录那些已经查找过
for(int i = 0 ; i < s1.length()+1 ;i++)//这里+1是为了防止find越界
{
vector<int> temp(s2.length()+1,0);
isSearch.push_back(temp);
}
dfs(s1,s2,s3,0,0,0,isSearch);
return isfind;
}
void dfs(string& s1, string& s2,string& s3,int i,int j,int k,vector<vector<int>>& isSearch)
{
if(i == s1.length()&& j==s2.length()&&k==s3.length()) {isfind=true;return;}
if(find[i][j] == 1) return;
find[i][j] = 1;
if(isfind) return;//如果找到了就不需要递归了
if(s1[i]==s3[k]&&i<s1.length()) dfs(s1,s2,s3,i+1,j,k+1,isSearch);//情况1
if(s2[j]==s3[k]&&j<s2.length()) dfs(s1,s2,s3,i,j+1,k+1,isSearch);//情况2
}
};
【一天一道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
原题地址 转化为二维地图游走问题. 比如s1="abab",s2="aab",s3="aabaabb",则有如下地图,其中"^&q ...
- 【LeetCode】97. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 【一天一道LeetCode】#8. String to Integer (atoi)
一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
- 【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 ...
随机推荐
- Ubuntu14.04安装 HP DeskJet GT 5820 打印机的方法
原创内容发表在 http://www.linuxdiyf.com/linux/25331.html 步骤就是: 这里(https://sourceforge.net/projects/hplip/fi ...
- C# 虹软SDK视频人脸识别和注册
一,准备工作 1.Afoge视频参数类 using AForge.Video.DirectShow; using System; using System.Collections.Generic; u ...
- 如何joomla修改版权信息
1.在language\zh_CN目录下有一个zh-CN.mod_footer.ini文件,修改里面的内容: 2.具体模板的html\mod_footer目录下的default.php文件内(具体文件 ...
- 判断当前设备是移动端或者PC端
<script> function browserRedirect() { var sUserAgent = navigator.userAgent.toLowerCase(); var ...
- REACT相关资料合集
===实例=== https://github.com/jesseskinner/react-webpack-demo ===UI组件库=== https://github.com/amazeui/a ...
- [转]关于OpenGL的绘制上下文
[转]关于OpenGL的绘制上下文 本文转自(http://www.cnblogs.com/Liuwq/p/5444641.html) 什么是绘制上下文(Rendering Context) 初学Op ...
- JavaScript 调试
在编写 JavaScript 时,如果没有调试工具将是一件很痛苦的事情. JavaScript 调试 没有调试工具是很难去编写 JavaScript 程序的. 你的代码可能包含语法错误,逻辑错误,如果 ...
- Apache ActiveMQ实战(2)-集群
ActiveMQ的集群 内嵌代理所引发的问题: 消息过载 管理混乱 如何解决这些问题--集群的两种方式: Master slave Broker clusters ActiveMQ的集群有两种方式: ...
- Save results to different files when executing multi SQL statements in DB Query Analyzer 7.01
1 About DB Query Analyzer DB Query Analyzer is presented by Master Genfeng,Ma from Chinese Mainl ...
- 深度学习与计算机视觉系列(3)_线性SVM与SoftMax分类器
作者: 寒小阳 &&龙心尘 时间:2015年11月. 出处: http://blog.csdn.net/han_xiaoyang/article/details/49949535 ht ...