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 ...
随机推荐
- VS2010修改默认配置路径
视图->属性管理器 弹出如下截图:
- SQL的经典操作——批量复制同行的其它列数据到其它列数据
看图说话比较直观: 对比复制前后的数据表: 使用SQL语句:UPDATE OR ROLLBACK Content SET YINBIAO = YINBIAO2, GESHU = GESHU2 WHER ...
- c++ 中的8种智能指针[转]
一.简介 由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete.程序员忘记 delete,流程太复杂,最终导致没有 delete,异常导致程序过早退出,没有执行 ...
- ASP.NET服务端基本控件介绍
ASP.NET服务端基本控件介绍 大概分为三种控件: HTML控件,ASP.NET把HTML控件当成普通字符串渲染到浏览器端,不去检查正确性,无法在服务端进行处理ASP.NET服务端控件,经过ASP. ...
- 曾经的10道JAVA面试题
1.HashMap和Hashtable的区别. 都属于Map接口的类,实现了将惟一键映射到特定的值上.HashMap 类没有分类或者排序.它允许一个null 键和多个null 值.Hashtable ...
- php ini_set('display_errors', $value)
正常情况下,在开发模式中,把错误显示出来,方便纠正,但在布署模式中,就得把错误关闭: ini_set('display_errors', 1); // 开启 ini_set('display_erro ...
- [C#]异步委托使用小计
APM(=Asynchronous Programming Model(=异步编程模型)) 使用 IAsyncResult 设计模式的异步操作是通过名为 Begin操作名称 和 End操作名称 的两个 ...
- 用Python实现的一个简单的随机生成器
朋友在ctr工作,苦于各种排期神马的,让我帮他整一个xxxx管理系统 里面在用户管理上面需要有一个批量从文件导入的功能,我肯定不能用汉字来作唯一性约束,于是想到了随机生成. 我首先想到的是直接用ite ...
- 在虚拟机的linux中利用VMware Tools实现与windows共享文件
很多人都知道安装"VMware Tools"可以实现与windows共享,但是其实它的功能远不止此.安装了"VMware Tools"后,虚拟机的网络. ...
- 在 IIS MIME 类型中添加 md 扩展名
最近在了解 Knowledge Base (知识库)的内容,对两个平台比较感兴趣,一个是 Raneto,一个是 MDwiki,两者都是使用md文件作为内容存储. 需要注意的是,使用IIS部署网站后,需 ...