【leetcode】44. Wildcard Matching
题目如下:
解题思路:本题和【leetcode】97. Interleaving String非常相似,同样可以采用动态规划的方法。记dp[i][j] = 1或者0 表示pattern[0:i]是否匹配string[0:j] ,如果pattern[i] == string[j] 或者 pattern[i] == '?',那么dp[i][j] = dp[i-1][j-1];如果pattern[i] = '*' 则复杂一些,因为'*'可以匹配s[j-1],s[j] 或者不匹配,因此只要满足其中任意一种情况都视为匹配,得出递推关系式:dp[i][j] = max(dp[i-1][j-1],dp[i][j-1],dp[i-1][j])。
代码如下:
class Solution(object):
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
np = '' #合并连续出现的*,提高效率
for i in p:
if len(np) == 0:
np += i
elif np[-1] == i and i == '*':
continue
else:
np += i #pattern和string都加上'#'开头,处理pattern以*开头,但是又不做匹配的情况
np = '#' + np
p = np
s = '#' + s dp = [[0] * len(s) for i in p]
#print dp dp[0][0] = 1 for i in range(len(dp)):
for j in range(len(dp[i])):
if p[i] == '*':
if i > 0 and j > 0:
dp[i][j] = max(dp[i-1][j-1],dp[i][j-1],dp[i-1][j])
elif i > 0 and j == 0:
dp[i][j] = dp[i-1][j]
elif i == 0 and j > 0:
dp[i][j] = dp[i][j-1]
elif i > 0 and j > 0 and (p[i] == '?' or p[i] == s[j]):
dp[i][j] = dp[i-1][j-1]
#print dp
return dp[-1][-1] == 1
【leetcode】44. Wildcard Matching的更多相关文章
- 【LeetCode】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- 【一天一道LeetCode】#44. Wildcard Matching
一天一道LeetCode系列 (一)题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches a ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【leetcode】Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- 【LeetCode】1023. Camelcase Matching 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+字典 日期 题目地址:https://leet ...
- 【LEETCODE】44、509. Fibonacci Number
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【leetcode】1023. Camelcase Matching
题目如下: A query word matches a given pattern if we can insert lowercaseletters to the pattern word so ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- LeetCode - 44. Wildcard Matching
44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...
随机推荐
- Docker 里面新建mysql 容器
1.获取MySQL镜像, a.直接从docker hub 下载docker镜像 docker pull +镜像名称 b.从别的项目上把镜像export出来 dockr load i + 镜像的TAR ...
- .Net-WCF-图书:《WCF编程》
ylbtech-.Net-WCF-图书:<WCF编程> <WCF编程>是2008年1月机械工业出版社出版的图书,作者是Juval Lowy.Clemens Vasters. 1 ...
- Visual Studio Code - 快捷键
默认快捷键 Visual Studio Code 默认快捷键 代码提示(自动补全,自动完成) 默认是快捷键是Ctrl+Space,与搜狗输入法切换中英文的快捷键冲突了..可以改搜狗输入法的快捷键(Sh ...
- 普通ACL访问控制列表
配置OSPF R1: R2: R3: R4: 在R1上查看OSPF的学习 测试R1与R4环回接口连通性 配置普通ACL访问控制列表: 先在R4配置密码用R1与R4建立telnet建立 密码huawei ...
- nodejs基础-HTTP
案例通过nodejs编写http服务程序 步骤:1,加载http模块2.创建http服务3.为http服务对象添加request事件处理程序4·开启http服务监听,准备接收客户端请求注意:1,浏览器 ...
- .net 学习官网
https://docs.microsoft.com
- 【洛谷p1025】数的划分
数的划分[传送门] 算法的话,dfs+剪枝: 据说是01年之前的NOIp提高组: 思路: 这道题是求把n无序的划分成k份的方案数,最直接的搜索方法是依次枚举x1,x2……xk的值,然后判断,显然这么搜 ...
- [Bzoj3224][Tyvj1728] 普通平衡树(splay/无旋Treap)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3224 平衡树入门题,学习学习. splay(学习yyb巨佬) #include<b ...
- [暑假集训Day4T1]羊圈
ZYC同志开农场了????? 二分答案. 对于每一个二分出来的答案对其进行检查(check),检查是否有一个长度大于m的字段和的平均值大于mid.方法如下:先把原数组的每一个元素减去mid,储存进一个 ...
- jquery动态加载select选项
$("#selectid").get(0).options.add(new Option(json数据))