【一天一道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 ...
随机推荐
- WPF 窗口居中 & 变更触发机制
窗口居中 & 变更触发机制 解决: 1.单实例窗口,窗口每次隐藏后再显示时,位置居中显示 2.多屏幕下单实例窗口,当父窗口移动到其它屏幕时,单实例窗口再次弹出时,位置才更新到父窗口屏幕. 3. ...
- Ubuntu14.04和Windows双系统时无法挂载磁盘解决方法
基本状况:我电脑Ubuntu14.04 和 Windows10 双系统,一个固态磁盘,一个机械磁盘.Ubuntu14.04装固态里面了,固态里没有Windows内容. 问题:Ubuntu14.04系统 ...
- iOS支付宝,微信,银联支付集成封装调用(下)
一.越来越多的app增加第三方的功能,可能app有不同的页面但调用相同的支付方式,例如界面如下: 这两个页面都会使用第三方支付支付:(微信,支付宝,银联)如果在每一个页面都直接调用第三方支付的接口全部 ...
- Protobuf3语法详解
定义一个消息类型 先来看一个非常简单的例子.假设你想定义一个"搜索请求"的消息格式,每一个请求含有一个查询字符串.你感兴趣的查询结果所在的页数,以及每一页多少条查询结果.可以采用如 ...
- ACM 数塔
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? 已经告诉你了,这是个DP的题 ...
- Nodejs 模块查找机制还不错(从当前目录开始逐级向上查找node_modules)
比如 m.js是能够调用a.js的, 这样子目录就可以避免重复安装node_modules. 够用了.
- Bootstrap3 栅格系统-列排序
通过使用 .col-md-push-* 和 .col-md-pull-* 类就可以很容易的改变列(column)的顺序. <div class="row"> <d ...
- iOS使用自签名证书实现HTTPS请求
概述 在16年的WWDC中,Apple已表示将从2017年1月1日起,所有新提交的App必须强制性应用HTTPS协议来进行网络请求. 默认情况下非HTTPS的网络访问是禁止的并且不能再通过简单粗暴的向 ...
- Core Python Programming一书中关于深浅拷贝的错误
该书关于深浅拷贝的论述: 6.20. *Copying Python Objects and Shallow and Deep Copies "when shallow copies are ...
- Android必知必会-获取View坐标和长宽的时机
如果移动端访问不佳,请访问–>Github版 背景 最近要实现一个功能,用到了一些属性动画,需要获取一些View的坐标信息,设计图如下: 这里我使用的是DialogFragment来实现的,可以 ...