【一天一道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 ...
随机推荐
- MySQL的Explain关键字查看是否使用索引
explain显示了MySQL如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句.简单讲,它的作用就是分析查询性能. explain关键字的使用方法很简单,就是 ...
- 58. Length of Last Word(easy, 字符串问题)
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Jmeter_ForEach控制器实现网页爬虫
一直以来,爬虫似乎都是写代码去实现的,今天像大家介绍一下Jmeter如何实现一个网页爬虫! Jmeter的爬虫原理其实很简单,就是对网页提交一个请求,然后把返回的所有href提取出来,利用ForEac ...
- 预习视频day1
课前预习 编译型和解释型语言优缺点 python2,pyhon3的宏观(大环境下)区别 python的种类 python编码规则 变量命名规范,常量 注释 1.编译型语言:将源码一次性转化为二进制代码 ...
- C++雾中风景8:Lambda表达式
上一篇C++的博客是Long Long ago了,前文讲到在看Lambda表达式的内容.笔者首次接触Lambda表达式应该是学习Python语言的时候,当时也不太明白这种表达方式的精髓,后续接触了Sc ...
- PHP MySQL 简介
PHP MySQL 简介 通过 PHP,您可以连接和操作数据库. MySQL 是跟 PHP 配套使用的最流行的开源数据库系统. 如果想学习更多 MySQL 知识可以查看本站MySQL 教程. MySQ ...
- python模块:网络协议和支持
python模块:网络协议和支持 webbrowser 调用浏览器显示html文件 webbrowser.open('map.html') [webbrowser - Convenient Web-b ...
- 动手试试Android Studio插件开发
由于业务关系,经常需要写一些表单页面,基本也就是简单的增删改查然后上传,做过几个页面之后就有点想偷懒了,这么低水平重复性的体力劳动,能不能用什么办法自动生成呢,查阅相关资料,发现android stu ...
- Oracle 11g客户端及PLSQL Developer配置|Instant Client Setup-64位|OraClientLite11g_x86
转载自:http://blog.csdn.net/xiaoyw71/article/details/45311589 Oracle 11g客户端 资源 下载资源,直接解压进行配置 Oracle官方资源 ...
- 21 RadioGroup ListFragment
结构 MainActivity.java package com.qf.day21_radiogroupfragment_demo3; import java.util.ArrayList; impo ...