[LeetCode] Interleaving String [30]
题目
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.
解题思路
交差字符串。给3个字符串s1, s2, s3,推断s3是不是由s1和s2组成的交叉字符串。
设s1长度为m, s2长度为n,推断 s3[0...m+n-1] 是不是由s1[0...m-1], s2[0...n-1]组成的交叉字符串,如果s1[m-1] == s3[m+n-1],则仅仅需推断s3[0...m+n-2]是不是由s1[0...m-2], s2[0...n-1]组成的交叉字符串...,依次这样推断下去。从这能够总结,这个问题能够划分为比它小的问题,这里使用动态规划应该比較合适。
dp[i][j]:表示s3[i+j-1] 是不是 由s1[0...i-1], s2[0...j-1]组成的交叉字符串。
dp[i][j] = dp[i][j] || dp[i-1][j] ; (s1[i-1]==s3[i+j-1])
dp[i][j] || dp[i][j-1] ; (s2[j-1]==s3[i+j-1])
代码实现
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
int m = s1.size();
int n = s2.size();
if(n+m != s3.size()) return false;
vector<vector<bool> > dp(m+1, vector<bool>(n+1, false));
//初始化dp[i][0]
for(int i=0;i<m; ++i){
if(s1[i] == s3[i])
dp[i+1][0] = true;
}
//初始化dp[0][i]
for(int i=0; i<n; ++i){
if(s2[i] == s3[i])
dp[0][i+1] = true;
}
dp[0][0] = true;
int k;
for(int i=1; i<=m; ++i)
for(int j=1; j<=n; ++j){
k = i+j;
if(s1[i-1] == s3[k-1])
dp[i][j] = dp[i][j] || dp[i-1][j];
if(s2[j-1] == s3[k-1])
dp[i][j] = dp[i][j] || dp[i][j-1];
}
return dp[m][n];
}
};
另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
)
[LeetCode] Interleaving String [30]的更多相关文章
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- [LeetCode] 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
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- [LeetCode] 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 @ Python
原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- LeetCode之“动态规划”:Interleaving String
题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...
随机推荐
- C++11里面的Lambda表达式
Lambda Expressions in C++ C++中的Lambda表达式 In Visual C++, a lambda expression—referred to as a lambda— ...
- 笔记--cocos2d-x 3.0 环境搭建
一.下载资源工具 1.下载cocos2d-x 3.0 官网地址:http://www.cocos2d-x.org/filedown/cocos2d-x-3.0-cn 2.下载VS2012 地址网上搜 ...
- Boost线程库学习笔记
一.创建一个线程 创建线程 boost::thread myThread(threadFun); 需要注意的是:参数可以是函数对象或者函数指针.并且这个函数无参数,并返回void类型. 当一个thre ...
- LR脚本自定义显示Controller虚拟用户状态
在场景监控的过程中,想知道场景运行时Vusers的运行状态以及每一个Vuser虚拟用户在本次场景运行的过程共迭代了多少次,那么就需要在VuGen脚本中自定义显示虚拟用户状态信息. 代码如下: stat ...
- Tomcat 常见问题篇
Tomcat 常见问题一.Tomcat常见问题 1.Tomcat web容器出现故障时,我们通过Tomcat自带的logs查看原因,以下错误提示都是源于logs 2.Connection refuse ...
- Asp.net开发常用的51个非常实用的代码
1.弹出对话框.点击转向指定页面 Code: Response.Write("<script>window.alert('该会员没有提交申请,请重新提交!')</scrip ...
- jquery.validate的使用
在页面上面引用 <script type="text/JavaScript" src="js/jQuery.js"></script> ...
- webservice和.net remoting浅谈
服务器端向客户端发送一个进程编号,一个程序域编号,以确定对象的位置. webservice和.net remoting都是用来通信的框架,它们最大的优点是可以像调用本地对象一样调用远程对象,比如: ...
- iOS 面试题 3
0.请写出代码,用blocks来取代上例中的protocol,并比较两种方法的优势.实际应用部分?请写出代码,用blocks取代协议或回调方法 声明: #import <Foundation/F ...
- ConcurrentQueue对列的基本使用方式
队列(Queue)代表了一个先进先出的对象集合.当您需要对各项进行先进先出的访问时,则使用队列.当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队. ConcurrentQueue< ...