97. 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.
s1 s2 内部字母相对顺序不变 是否可以混合成s3
二维动态规划,s3中的第i+j个字符一定是由s1中的前i个字符和s2中的前j个字符组合而成 第i+j个字符可能是s1中的第i个也可能是s2中的第j个
也可以从后往前分析,s3中的最后一个字符一定是s1或者s2的最后一个字符,假如是s2 那么对应位置为true,s2的索引往前移动一位,s3的索引向前移动一位 继续遍历
hollyshit leetcode检查速度太慢一直submit failed,先把代码放这 思路大致是这样的
可注意if的条件要有 dp[i][j-1] dp[i-1][j]==true 以及对长度这个基本条件的判断
public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if((s1==null&&(s2==s3))||(s2==null&&(s1==s3))) return true;
int len1=s1.length();
int len2=s2.length();
if(len1+len2!=s3.length()) return false;
boolean dp[][]=new boolean[len1+1][len2+1];
dp[0][0]=true;
for(int i=0;i<=len1;i++){
for(int j=0;j<=len2;j++){
if(dp[i][j]==true
||(i>=1&&dp[i-1][j]==true&&(s1.charAt(i-1)==s3.charAt(i+j-1)))
||(j>=1&&dp[i][j-1]==true&&(s2.charAt(j-1)==s3.charAt(i+j-1))))
dp[i][j]=true;
else dp[i][j]=false;
}
}
return dp[len1][len2];
}
}
97. Interleaving String相关推荐
- 【重点!DP】LeetCode 97. Interleaving String
LeetCode 97. Interleaving String 参考网址:[1]https://www.youtube.com/watch?v=HmAF9xeS_2I [2]http://www.c ...
- 【leetcode】97. Interleaving String
题目如下: 解题思路:本题可以采用动态规划的方法.记dp[i][j]表示用s1的前i个字符和s2的前j个字符能否组成s3的前(i+j)个字符,如果dp[i-1][j]是True的话,只要s1[i] = ...
- 97. Interleaving String 交错字符串
Title 给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的. 示例 1: 输入: s1 = "aabcc", s2 = "dbb ...
- LeetCode Interleaving String(动态规划)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- 【Lintcode】029.Interleaving String
题目: Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2 ...
- 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
1. 是一个很明显的动态规划题. 2. s3中的每个字符不是s1中的就是s2中的,只要根据它之前的状态做转移就可以. 1 class Solution { 2 public: 3 bool isInt ...
- LeetCode Interleaving String
原题链接在这里:https://leetcode.com/problems/interleaving-string/ 这是一道DP题,用boolean数组dp维护历史数据,dp[i][j]表示长度为i ...
- leetcode解题报告:Interleaving String
问题描述: 给定两个字符串s1, s2,判断字符串s3是否是由s1.s2交错构成. 例子: s1 = "aabcc", s2 = "dbbca", When s ...
最新文章
- php 数据钻取,多层钻取 - 海致BDP帮助中心、帮助文档
- MyBatis 实际使用案例-dataSource
- matlab之reshape函数
- myeclipse和maven的clean和build
- 从输入一个url到浏览器页面展示都经历了哪些过程?
- 让Python输出更漂亮
- python写Bicubic方法,跑数据集Set5和Set14,保存PSNR和SSIM的值
- np.minimum()与tf.minimum()的用法
- 今天七夕给大家送个“对象”吧!拿走不谢!
- js的tree转数组
- Linux宝塔面板命令大全,快速学会
- 智能网联汽车风险评估方法EVITA
- Linux awk 命令详解
- OpenCV项目实战日志——检测文字并对比识别
- 计算机科学与技术职业生涯规划,计算机科学与技术职业生涯规划ppt
- 贺利坚的课程教学链接
- 机器视觉的来源与发展
- 大满足!史上最全|托盘类自动化立体库视频合集
- 利用rect函数截取原图的图像OPENCV
- github java排课系统_团队-排课系统-开发文档