[抄题]:

给出三个字符串: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 又是热脸贴冷屁股

  1. public class Solution {
  2. /*
  3. * @param s1: A string
  4. * @param s2: A string
  5. * @param s3: A string
  6. * @return: Determine whether s3 is formed by interleaving of s1 and s2
  7. */
  8. public boolean isInterleave(String s1, String s2, String s3) {
  9. //corner case
  10. if (s1.length() + s2.length() != s3.length()) {
  11. return false;
  12. }
  13. //state
  14. boolean[][] interleaved = new boolean[s1.length() + 1][s2.length() + 1];
  15. interleaved[0][0] = true;
  16. //initialization
  17. //s1 == 0
  18. for (int i = 1; i <= s2.length(); i++) {
  19. if (s2.charAt(i - 1) == s3.charAt(i - 1) && interleaved[0][i - 1]) {//i -1 &&
  20. interleaved[0][i] = true;
  21. }
  22. }
  23. //s2 == 0
  24. for (int j = 1; j <= s1.length(); j++) {
  25. if (s1.charAt(j - 1) == s3.charAt(j - 1)&& interleaved[j - 1][0]) {//
  26. interleaved[j][0] = true;
  27. }
  28. }
  29. //function
  30. for (int i = 1; i <= s1.length(); i++) {
  31. for (int j = 1; j <= s2.length(); j++) {
  32. if (s1.charAt(i - 1) == s3.charAt(i + j - 1) && interleaved[i - 1][j]) {
  33. interleaved[i][j] = true;
  34. }else if (s2.charAt(j - 1) == s3.charAt(i + j - 1) && interleaved[i][j - 1]) {
  35. interleaved[i][j] = true;
  36. }else {
  37. interleaved[i][j] = false;
  38. }
  39. }
  40. }
  41. //answer
  42. return interleaved[s1.length()][s2.length()];
  43. }
  44. }

交叉字符串 · 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. centos 7.5 安装mysql

    1.Mysql: 在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB. 1.下载并安装MySQL官 ...

  2. tornado.ioloop.IOLoop相关文章

    http://6167018.blog.51cto.com/6157018/1532899 http://kenby.iteye.com/blog/1159621

  3. oracle 年龄计算 岁 月 天

    select trunc(months/12) || '岁' || trunc(mod(months, 12)) || '月' ||       trunc(sysdate - add_months( ...

  4. BCGcontrolBar(五) 对话框大小改变控件自动适应

    改变控件大小 首先在 构造函数中加入 EnableLayout(); 在OnInitDialog()函数中加入 CBCGPStaticLayout* pLayout = (CBCGPStaticLay ...

  5. guess_age

    age_shanshan = 18count = 3num = 0while num < count: age = int(input("age:")) if age == ...

  6. 使用nproxy代理本地服务到内网

    前端开发中:很多场景需要在局域网下的其他手机或设备查看网页, 问题来了, web服务部署在本机的某个端口上(8080),只能通过本机浏览器访问,  怎样能让局域网下的其他设备也访问呢?可能你会说 关闭 ...

  7. 10.Action中的method属性

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 在struts1.x中我们知道通过继承DispatchAction可以实现把 ...

  8. shiro与threamleaf的整合

    1.添加依赖 2.在配置类中添加shiroDialect

  9. eclipse 断点找到同名的其它类

    转载自Eclipse断点进入另一个项目的同名Java文件中(http://tunps.com/p/11789.html) eclipse 断点找到同名的其它类 A和B是两个相同的项目,A一直本地,B是 ...

  10. JAVA NIO学习记录1-buffer和channel

    什么是NIO? Java NIO(New IO)是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java IO API.NIO与原来的IO有同样的作用和目的,但是使用的方式完全不 ...