【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 --------------------------------------------------------------- ...
随机推荐
- flask-script实现自动刷新页面调试
本文flask==1.0.2 1.导入extension包 from flask_script import Manager 2.使用manager管理工具 app = Flask(__name__) ...
- 仅对原表新增列的全量数据.csv
w
- [转]Scikit-learn使用总结
1 scikit-learn基础介绍 1.1 估计器(Estimator) 估计器,很多时候可以直接理解成分类器,主要包含两个函数: fit():训练算法,设置内部参数.接收训练集和类别两个参数. p ...
- [Linux] 015 用户管理命令
1. 用户管理命令:useradd 命令名称:useradd 命令所在路径:/bin/sbin/useradd 执行权限:root 语法:useradd 用户名 功能描述:添加新用户 范例: $use ...
- centos7系统乱码问题解决
操作步骤: 查看当前系统的默认语言 echo $LANG 查看系统支持的语言库 locale 如果没有要设置的语言需要安装一下 yum groupinstall chinese-support -y ...
- (vue.js)axios interceptors 拦截器中添加headers 属性
(vue.js)axios interceptors 拦截器中添加headers 属性:http://www.codes51.com/itwd/4282111.html 问题: (vue.js)axi ...
- 红玫瑰&爱情转移
- python中bytes和str
1.python中bytes和str Python3 最重要的新特性大概要算是对文本(text)和二进制数据(binary data)作了更为清晰的区分 (1)Python 3.0使用文本和(二进制) ...
- pytest--命令行参数
使用pytest --help可以查看全部选项 -v:pytest -v 说明:可以输出用例更加详细的执行信息,比如用例所在的文件及用例名称等 -q(--quiet) 该选项的作用与-v/--ve ...
- ORACLE数据字典DBA视图
select * from DBA_ALL_TABLES --显示数据库中所有表的描述 select * from DBA_CATALOG --列出所有数据库标,视图,同义词和序列 ...