交叉字符串 · Interleaving String
[抄题]:
给出三个字符串:s1、s2、s3,判断s3是否由s1和s2交叉构成。(洗牌)
比如 s1 = "aabcc" s2 = "dbbca"
- 当 s3 = "aadbbcbcac",返回 true.
- 当 s3 = "aadbbbaccc", 返回 false.
[思维问题]:
- 不知道怎么表示交叉。分析要用三维数组,i j k。但是分析j 是否等于i+j,就只用二维数组了。
- 不知道为什么看最后一位:第一位不知道是谁出的,但是最后一位非此即彼,可以用来递推。
- 不知道状态函数和判断字符函数的关系:二者是要分开的。
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 行列初始化的规则和普通递归的规则一样,状态函数和判断字符函数都有且分开。
- 序列dp。第0位初始化,i = 1,i<=n时操作。因此,f[0][0]也要初始化
- 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的更多相关文章
- [Swift]LeetCode97. 交错字符串 | Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...
- lintcode 中等题:interleaving String 交叉字符串
题目 交叉字符串 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例 比如 s1 = "aabcc" s2 = "dbbca" - 当 ...
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- [LeetCode] Interleaving String [30]
题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- 40. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 【一天一道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】97. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
随机推荐
- 被折腾的sql编程
- SVM的sklearn.svm.SVC实现与类参数
SVC继承了父类BaseSVC SVC类主要方法: ★__init__() 主要参数: C: float参数 默认值为1.0 错误项的惩罚系数.C越大,即对分错样本的惩罚程度越大,因此在训练样本中准确 ...
- python Web开发你要理解的WSGI & uwsgi详解
原文:https://www.jb51.net/article/144852.htm WSGI协议 首先弄清下面几个概念: WSGI:全称是Web Server Gateway Interface,W ...
- ElasticSearch 索引模块——全文检索
curl -XPOST http://master:9200/djt/user/3/_update -d '{"doc":{"name":"我们是中国 ...
- Linux常用命令的命名来源
很多人在学习Linux的时候会疑惑:这么多的Linux名,他们都是怎么被定义的?林纳斯是怎么制定如此花样繁多且数量庞大的命令?今天这篇文章可能会帮你解开疑惑. ## 1. 目录缩写 缩写 | 全称 | ...
- js 解决图片居中问题
下述方法能够解决图片居中问题: (1)宽一些或者高一些(相对父元素的大小):图片在父元素的可视范围内显示图片的中间位置 (2)小一些(相对父元素的大小):图片在父元素的可视范围内居中显示 实现原理:根 ...
- express基础
express框架 const express = require("express"); 引入express框架 var app= express(); 实例化 相当于构造函 ...
- mybatis匹配字符串的坑
where语句中我们经常会做一些字符串的判断,当传入的字符串参数为纯数字时,在mybatis的条件语句test里匹配全数字字符串需要注意会有如下现象: 所以里面的字符串需要加单引号,mybatis是匹 ...
- 20165233 2017-2018-2 《Java程序设计》第八周学习总结
20165233 2017-2018-2 <Java程序设计>第八周学习总结 教材学习内容总结 基础:Java中的线程,Thread类与线程的创建 - 线程是比进程更小的单位. - JVM ...
- CENTOS系统安装及初始化配置相关
一,配置网卡: 1,设置网卡ip地址:vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0TYPE=EthernetONBOOT=yes ...