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 ...
随机推荐
- 【培训】Linux笔记 自学
1.关机 查看在线用户 who:查看网络联机状态 netstat -a:查看后台执行程序 ps -aux 关机 shutdown -h now.init 0 halt.poweroff 硬件关机 重启 ...
- (hdu)5391 Zball in Tina Town
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5391 Problem Description Tina Town is a friendl ...
- 深度优化LNMP之Nginx (转)
深度优化LNMP之Nginx Nginx基本安全优化 1.调整参数隐藏Nginx版本号信息 一般来说,软件的漏洞都和版本有关,因此我们应尽量隐藏或清除Web服务队访问的用户显示各类敏感信息(例 ...
- php单引号和双引号的区别与用法
php里的单引号把内容当成纯文本,不会经过服务器翻译.而双引号则与此相反.里面的内容会经过服务器处理(process). 举个简单的例子: $foo="data"; echo ...
- 织梦DedeCMS网站地图模板
亲和百度蜘蛛,分页多层次特色,织梦系统最好用的网站地图! 用 DedeCMS(织梦) 系统搭建的网站多数都是以优化为主要目标的网站类型,既然是优化站 SEO 手段就离不开为网站设置网站地图.可是 De ...
- CakePHP采用model的save方法更新数据所需查询
采用model的save方法更新数据所需查询 1. 验证时候要确认是update 或者 create,以便使用对应规则 public $validate = array( 'field_name' = ...
- ODBC方式连接Informix数据库
公司某个报表系统使用Informix数据库,在谋划使用Perl语言写数据采集程序后,花费了很多时间建立Perl访问Informix连接.恰巧Windows下ActivePerl的CPAN中又没有DBD ...
- 【C语言】中的布尔类型
C语言中的布尔类型 一.相关基础知识 首先bool true false为C++中的关键字,C语言中默认不支持这几个字符! 二.具体内容 在C89 (ANSI C)标准中没有定义与布尔类型相关的内 ...
- after I see Little Dorrit
也许是我太追名逐利,所以我不肯承认自己花费了大把的时间看电影,通过写博客好像自己从中感悟到了什么,好像看电影也是一种学习的方式. 也许是我平静自内心的方式,我太忙于玩或者学习,甚至没有机会非常沉静 一 ...
- POJ 3267 The Cow Lexicon 简单DP
题目链接: http://poj.org/problem?id=3267 从后往前遍历,dp[i]表示第i个字符到最后一个字符删除的字符个数. 状态转移方程为: dp[i] = dp[i+1] + 1 ...