【原创】leetCodeOj --- Interleaving String 解题报告
题目地址:
https://oj.leetcode.com/problems/interleaving-string/
题目内容:
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。若s1[index1] == s3[index3]则 index1 ++ && index3 ++。反之若s2[index2] == s3[index3]则 index2 ++ && index3 ++。
但是这有个问题,就是如果s2[index2] == s1[index1]的话,选择谁呢?
答案是谁都选一次看看。于是,我们又要动态规划了。。
设dp[m][n] 为s1从下标m开始,s2从下标n开始是否能匹配下标从m + n开始的s3 (为什么是m + n?因为s1 m下标以前的字符和s2 n下标以前的字符都已经匹配过了)。如果值为1,那么可以匹配;如果为2,那么不能匹配。如果为0,那么需要递归调用子问题来解决。
简单看看动态规划方程:
dp[m][n] = {
s1[m] == s2[n] == s3[m+n] : dp[m][n+1] || dp[m+1][n]; // 若s1和s2当前都可以匹配,就都试一下,谁行谁就上。短路
s1[m] == s3[m+n] : dp[m+1][n]; // 仅当s1可以匹配
s2[n] == s3[m+n] : dp[m][n+1]; // 仅当s2可以匹配
else : false; // 谁都无法匹配
}
全部代码:
class Solution {
public:
string t1,t2,t3;
char dp[][]; // 没处理过就是0,处理了可以就是1,不行就是2
bool isInterleave(string s1, string s2, string s3) {
int len1 = s1.size();
int len2 = s2.size();
int len3 = s3.size();
if (len1 + len2 != len3)
return false;
if (len1 == )
return s2 == s3 ? true : false;
if (len2 == )
return s1 == s3 ? true : false;
t1 = s1;
t2 = s2;
t3 = s3;
return trueStuff(,,len1,len2);
}
bool trueStuff(int m,int n,int len1,int len2) // m is index for s1,n is index for s2.
{
if (m >= len1 && n >= len2)
return true;
if (dp[m][n] != )
return dp[m][n] == ? true : false;
bool tmp = false; // mark if dp[m][n] can do the right thing.
if (m >= len1)
tmp = (t2[n] == t3[m+n] ? trueStuff(m,n + ,len1,len2) : false);
else if (n >= len2)
tmp = (t1[m] == t3[m+n] ? trueStuff(m + ,n,len1,len2) : false);
else if (t1[m] == t2[n] && t1[m] == t3[m+n])
tmp = (trueStuff(m + ,n,len1,len2) || trueStuff(m,n + ,len1,len2));
else if (t1[m] == t3[m+n])
tmp = trueStuff(m + ,n,len1,len2);
else if (t2[n] == t3[m+n])
tmp = trueStuff(m,n + ,len1,len2);
else
tmp = false;
dp[m][n] = (tmp == true ? : );
return tmp;
}
};
【原创】leetCodeOj --- Interleaving String 解题报告的更多相关文章
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】833. Find And Replace in String 解题报告(Python)
[LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【原创】leetCodeOj --- Largest Number 解题报告
原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, ...
- 【原创】leetCodeOj --- Min Stack 解题报告
题目地址: https://oj.leetcode.com/problems/min-stack/ 题目内容: Design a stack that supports push, pop, top, ...
- 【原创】leetCodeOj --- Dungeon Game 解题报告
原题地址: https://oj.leetcode.com/problems/dungeon-game/ 题目内容: The demons had captured the princess (P) ...
- 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)
题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...
- 【原创】leetCodeOj --- Sort List 解题报告
今日leetcode链表题全制霸 原题地址: https://oj.leetcode.com/problems/sort-list/ 题目内容: Sort List Sort a linked lis ...
随机推荐
- System单元对所有与COM相关的声明就这么多,需要倒背如流
是首先是VM表,但是和COM相关的函数地址都废弃了,这几个VM函数具体放在哪里,还得在研究: { Virtual method table entries } vmtSelfPtr = -; vmtI ...
- JavaScript之面向对象1
学习过Java程序的开发人员都知道面向对象是怎么回事. 面向对象无非就是封装.多态.继承 比如: 声明一个类: class Person{ //私有成员 private String name; pr ...
- 在web网页中正确使用图片格式
今天又看了一遍淘宝平四分享的PPT,以前转载网址:http://blog.sina.com.cn/s/blog_995c1f6301017fd2.html
- OCP读书笔记(15) - 管理SQL性能调优
SQL Tuning Advisor(STA): 使用oracle提供的程序包进行sql优化 SQL> conn scott/tiger SQL), name )); SQL> inser ...
- 免费git服务器以及使用过程中遇到的问题
1. git rm *,git pull会先git fetch后再git merge,更安全的做法是git fetch修改后再push:git remote rm origin 2. https:// ...
- Linux C 编程内存泄露检測工具(二):memwatch
Memwatch简单介绍 在三种检測工具其中,设置最简单的算是memwatch,和dmalloc一样,它能检測未释放的内存.同一段内存被释放多次.位址存取错误及不当使用未分配之内存区域.请往http: ...
- Django写的投票系统2(转)
在上一篇中 django实例:创建你的第一个应用投票系统(一) 已经介绍基本的功能,并已经启动服务了.这一节介绍数据库相关的东东. 首页打开mysite/settings.py配置文件, 设置数据库打 ...
- HDOJ 4249 A Famous Equation DP
DP: DP[len][k][i][j] 再第len位,第一个数len位为i,第二个数len位为j,和的第len位为k 每一位能够从后面一位转移过来,能够进位也能够不进位 A Famous Equat ...
- docker 的安装
官方站点上有各种环境下的 安装指南,这里主要介绍下Ubuntu和CentOS系列的安装. Ubuntu 系列安装 Docker 通过系统自带包安装 Ubuntu 14.04 版本号系统中已经自带了 D ...
- CSS3 3D旋转动画代码实例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...