[抄题]:

给出三个字符串:s1、s2、s3,判断s3是否由s1和s2交叉构成。(洗牌)

比如 s1 = "aabcc" s2 = "dbbca"

- 当 s3 = "aadbbcbcac",返回  true.

- 当 s3 = "aadbbbaccc", 返回 false.

[思维问题]:

  1. 不知道怎么表示交叉。分析要用三维数组,i j k。但是分析j 是否等于i+j,就只用二维数组了。
  2. 不知道为什么看最后一位:第一位不知道是谁出的,但是最后一位非此即彼,可以用来递推。
  3. 不知道状态函数和判断字符函数的关系:二者是要分开的。

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 行列初始化的规则和普通递归的规则一样,状态函数和判断字符函数都有且分开。
  2. 序列dp。第0位初始化,i = 1,i<=n时操作。因此,f[0][0]也要初始化
  3. dp中的数组长度一般直接写s1.length(),不用新取

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

行列初始化的规则和普通递归的规则一样,状态函数和判断字符函数都有且分开。

[复杂度]:Time complexity: O(n^2) Space complexity: O(n^2)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

343. Integer Break 最值dp

646. Maximum Length of Pair Chain 最值dp

[代码风格] :

else if 又是热脸贴冷屁股

public class Solution {
/*
* @param s1: A string
* @param s2: A string
* @param s3: A string
* @return: Determine whether s3 is formed by interleaving of s1 and s2
*/
public boolean isInterleave(String s1, String s2, String s3) {
//corner case
if (s1.length() + s2.length() != s3.length()) {
return false;
}
//state
boolean[][] interleaved = new boolean[s1.length() + 1][s2.length() + 1];
interleaved[0][0] = true;
//initialization
//s1 == 0
for (int i = 1; i <= s2.length(); i++) {
if (s2.charAt(i - 1) == s3.charAt(i - 1) && interleaved[0][i - 1]) {//i -1 &&
interleaved[0][i] = true;
}
}
//s2 == 0
for (int j = 1; j <= s1.length(); j++) {
if (s1.charAt(j - 1) == s3.charAt(j - 1)&& interleaved[j - 1][0]) {//
interleaved[j][0] = true;
}
}
//function
for (int i = 1; i <= s1.length(); i++) {
for (int j = 1; j <= s2.length(); j++) {
if (s1.charAt(i - 1) == s3.charAt(i + j - 1) && interleaved[i - 1][j]) {
interleaved[i][j] = true;
}else if (s2.charAt(j - 1) == s3.charAt(i + j - 1) && interleaved[i][j - 1]) {
interleaved[i][j] = true;
}else {
interleaved[i][j] = false;
}
}
}
//answer
return interleaved[s1.length()][s2.length()];
}
}

交叉字符串 · Interleaving String的更多相关文章

  1. [Swift]LeetCode97. 交错字符串 | Interleaving String

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...

  2. lintcode 中等题:interleaving String 交叉字符串

    题目 交叉字符串 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例 比如 s1 = "aabcc" s2 = "dbbca" - 当 ...

  3. [LeetCode] Interleaving String - 交织的字符串

    题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...

  4. [LeetCode] Interleaving String [30]

    题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...

  5. Leetcode:Interleaving String 解题报告

    Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...

  6. 40. Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  7. 【一天一道LeetCode】#97. Interleaving String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...

  8. LeetCode之“动态规划”:Interleaving String

    题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...

  9. 【LeetCode】97. Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

随机推荐

  1. javascript创建对象之原型模式(三)

    少废话,先上代码: function Human() { } Human.prototype.name = "成吉思汗"; Human.prototype.sex = " ...

  2. Windows10右键添加“在此处打开命令窗口"

    今天跑代码时用到了Powershell,有一个环境变量死活都找不到,无奈只好重新回到cmd命令行测试,结果立马就跑通了.-_-||  由于现在Win10默认右键只有Powershell,所以为了以后方 ...

  3. Spark分析之Master、Worker以及Application三者之间如何建立连接

    Master.preStart(){ webUi.bind() context.system.scheduler.schedule( millis, WORKER_TIMEOUT millis, se ...

  4. Linux 下的两种分层存储方案

    背景介绍 随着固态存储技术 (SSD),SAS 技术的不断进步和普及,存储介质的种类更加多样,采用不同存储介质和接口的存储设备的性能出现了很大差异.SSD 相较于传统的机械硬盘,由于没有磁盘的机械转动 ...

  5. C# 程序A发送Log记录给程序B,程序B处理和分析Log记录

    C# 程序A发送Log记录给程序B,程序B处理和分析Log记录 关键字:C# ;Log记录 ;在线Log记录;Socket:httplistener 一.常用场景 1. APP开发,在真机或者虚拟机上 ...

  6. 0_Simple__simpleTexture + 0_Simple__simpleTextureDrv

    使用纹理引用来旋转图片,并在使用了静态编译和运行时编译两种环境. ▶ 源代码:静态编译 #include <stdio.h> #include <windows.h> #inc ...

  7. Laravel之Eloquent ORM

    一.ORM编程思想 1.1 Active Record 设计模式 Active Record 是一种数据访问设计模式,它可以帮助你实现数据对象Object到关系数据库的映射.应用Active Reco ...

  8. python 网页爬虫,下载网络图片

    # coding=utf-8 import lxml,bs4,re,requests csvContent='' file = open('D:\\tyc_demo.html','rb') soup ...

  9. 进程队列(Queue),Pipe(管道), Manager 进行进程之间的数据传递和传输

    进程Queue,实现进程传输的队列 1.Queue from multiprocessing import Process, Queue def f(q): q.put('1') q.put('2') ...

  10. Mysql 触发器 A表记录到B表

    1:查询出需要的列名 备用 #列名 select COLUMN_NAME from information_schema.columns where TABLE_SCHEMA='yunpiaobox_ ...