lecode 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.
1.搜索的方法(超时)
public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
return isInter(s1,s2,s3,0,0,0);
}
public boolean isInter(String s1,String s2,String s3,int r1,int r2, int r3)
{
if(r3==s3.length()) return true;
boolean ans=false;
if(r1<s1.length()&&s1.charAt(r1)==s3.charAt(r3))
{
ans=isInter(s1,s2,s3,r1+1,r2,r3+1);
}
if(ans) return true;
if(r2<s2.length()&&s2.charAt(r2)==s3.charAt(r3))
{
ans=isInter(s1,s2,s3,r1,r2+1,r3+1);
return ans;
}
return false;
}
}
dp[i][j]表示s1前i 和s2前j个是否能组成s3的前i+j+1个, false 不能 true 能
dp[s1.len-1][s2.len-1] 就是我们的答案
dp[i][j]=dp[i-1][j]&&s1[i]==s3[i+j+1]|| dp[i][j-1]&&s1[j]==s3[i+j+1]
public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
char c1[]=s1.toCharArray();
char c2[]=s2.toCharArray();
char c3[]=s3.toCharArray();
int len1=s1.length();
int len2=s2.length();
int len3=s3.length();
if(len1+len2!=len3) return false;
if(len1==0) return s2.equals(s3);
if(len2==0) return s1.equals(s3);
boolean dp[][]=new boolean[s1.length()+1][s2.length()+1];
dp[0][0]=true;
for(int i=1;i<=len1;i++)
{
dp[i][0]=dp[i-1][0]&&c1[i-1]==c3[i-1];
}
for(int j=1;j<=len2;j++)
{
dp[0][j]=dp[0][j-1]&&c2[j-1]==c3[j-1];
}
for(int i=1;i<=len1;i++)
{
for(int j=1;j<=len2;j++)
{
dp[i][j]=dp[i-1][j]&&(c1[i-1]==c3[i+j-1]);
if(dp[i][j]) continue;
dp[i][j]=dp[i][j-1]&&(c2[j-1]==c3[i+j-1]);
}
}
return dp[len1][len2];
}
}
lecode Interleaving String的更多相关文章
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- 40. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 二维动态规划——Interleaving String
97. Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2 ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- LeetCode之“动态规划”:Interleaving String
题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- 【LeetCode】97. 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, Given: s1 ...
随机推荐
- 关于web开发的一点理解
对于web开发上的一点理解 1 宏观上的一点理解 网页从请求第地址 到获得页面的过程:从客户端(浏览器)通过地址 从soket把请求报文封装发往服务端 服务端通过解析报文并处理报文最后把处理的结果 ...
- 配置apache+trac环境
按照trac官网上的配置始终通不过.仔细看了,原来我们使用的apache版本是2.4的,在2.4中有些directive已经变了. 例如:原来的 Allow from all 现在变成了 Requir ...
- getDrawingRect,getHitRect,getLocalVisibleRect,getGlobalVisibleRect
本文主要大体讲下getHitRect().getDrawingRect().getLocalVisibleRect().getGlobalVisibleRect. getLocationOnScree ...
- ES6笔记-正则表达式和字符串正则方法
RegExp构造函数 在ES5中,RegExp构造函数的参数有两种情况. 第一种情况是,参数是字符串,这时第二个参数表示正则表达式的修饰符(flag). var regex = new RegExp( ...
- PHP数组操作——获取数组最后一个值的方法
php开发过程中,可能经常需要对取出的数组要获取数组的最后健或值.在这里总结了三个方法,并且跟据他们三个方法在一些情况下如何使用的条件限制进行了说明. <?php $array=array(1, ...
- node.js操作mongoDB数据库
链接数据库: var mongo=require("mongodb"); var host="localhost"; var port=mongo.Connec ...
- hadoop1中mapreduce原理详解
剖析Mapreduce作业运行机制:原理如下图: 原理图的解释的可以分为以下几个部分 1.客户端提交一个mapreduce的jar包给JobClient 2.JocClient通过RPC和JobTra ...
- phpstorm调整背景、字体颜色
从这个网站(http://phpstorm-themes.com/)下载各类主题的xml文件, 然后将文件放到phpStorm的文件夹中,比如:C:\Users\USERNAME\.PhpStorm2 ...
- Navicat Premium 未保存的SQL如何找回 ?
在使用 Navicat Premium 编辑SQL的过程中为防止程序意外崩溃,已经将编辑的SQL都已经备份. 备份存放目录地址:C:\Users\{登录用户名}\Documents\Navicat\M ...
- PHP利器-WAMPServer
为了配置memcacheAdmin,牵涉到搭建PHP的环境,使用的是WAMPServer,安装之后,需要对apache进行配置, 端口,需要将wamp\bin\apache\Apache2.2.21\ ...