[LeetCode 97] Interleaving String(Python)
来源:互联网 发布:民科笑话知乎 编辑:程序博客网 时间:2024/06/06 09:22
题目描述
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.
思路
动态规划思想。把问题转化为能否在一个二维数组中,从左上角走到右下角。
代码
class Solution(object): def isInterleave(self, s1, s2, s3): """ :type s1: str :type s2: str :type s3: str :rtype: bool """ if len(s2) + len(s1) != len(s3): return False res=[[0 for j in range(len(s2) + 1)] for i in range(len(s1) + 1)] # 代表从s1中取'',从s2中取''的情况 res[0][0] = 1 for i in range(len(s1) + 1): for j in range(len(s2) + 1): if i == 0 and j == 0: continue # 代表从s1中取'',从s2中不取''的情况 elif i == 0: res[i][j] = res[i][j - 1] & (s2[j - 1] == s3[j - 1]) # 代表从s2中取'',从s1中不取''的情况 elif j == 0: res[i][j] = res[i - 1][j] & (s1[i - 1] == s3[i - 1]) # 代表从s1中不取'',从s2中不取''的情况 else: res[i][j] = (res[i - 1][j] & (s1[i - 1] == s3[i + j - 1])) or (res[i][j - 1] & (s2[j - 1] == s3[i + j - 1])) return bool(res[len(s1)][len(s2)])
复杂度分析
时间复杂度
阅读全文
0 0
- leetcode 97 Interleaving String(python)
- [LeetCode 97] Interleaving String(Python)
- [Leetcode][python]Interleaving String
- LeetCode 97 Interleaving String
- LeetCode(97) Interleaving String
- [leetcode 97] Interleaving String
- [leetcode] 97 Interleaving String
- 【LeetCode】【97】Interleaving String
- leetcode || 97、Interleaving String
- [leetcode 97]Interleaving String
- leetcode 97: Interleaving String
- leetCode 97:Interleaving String
- Leetcode-97 Interleaving String
- Leetcode 97 Interleaving String
- LeetCode 97 Interleaving String
- Leetcode 97, Interleaving String
- Leetcode Interleaving String 97
- [Leetcode] #97 Interleaving String
- Django 发送邮件配置
- Windows 10中轻松调整显示器的4个简单步骤
- android:clipToPadding和android:clipChildren
- solr查询语法详解
- RDD的特点
- [LeetCode 97] Interleaving String(Python)
- Spring MVC 基于阻塞队列 LinkedBlockingQueue 的同步长轮询功能实现
- linux 网络命令
- 蓝牙核心系统架构介绍
- linux 命令行中的计算器
- 数据库视频总结 一
- [Android6.0][RK3399] TypeC 基础知识
- 【牛客】最小的k个数
- 【转载】SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)