【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 --------------------------------------------------------------- ...
随机推荐
- 去掉注释index.html
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...
- UVA 11752 The Super Powers(暴力)
题目:https://cn.vjudge.net/problem/UVA-11752 题解:这里只讨论处理越界的问题. 因为题目最上界是 264-1. 我们又是求次幂的. 所以当我们就可以知道 i 的 ...
- 设置VsCode自动换行
方法如下: 文件 -> 首选项 -> 设置 如果你是Mac则是右上角 Code -> 首选项 -> 设置 然后在右侧的编辑窗口中添加 1 "editor.wordWr ...
- STM32 ADC基础与多通道采样
12位ADC是一种逐次逼近型模拟数字数字转换器.它有多达18个通道,可测量16个外部和2个内部信号源.ADC的输入时钟不得超过14MHZ,它是由PCLK2经分频产生.如果被ADC转换的模拟电压低于低阀 ...
- http代理工具delphi源码
http://www.caihongnet.com/content/xingyexinwen/2013/0721/730.html http代理工具delphi源码 以下代码在 DELPHI7+IND ...
- C# 做延迟,但系统又能同时能执行其它任务
private void Delay(int Millisecond) //使用时直接调用即可 { DateTime current = DateTime.Now; while (current.Ad ...
- Easyui表格显示日期格式错误
问题 如下图: 解决办法: 以Sql Server 2008为例,在执行查询时,把“采购日期”字段由datetime转换为string,前端以string类型显示.
- Java8默认方法
Java8引入的接口默认方法实现一个新的概念.此功能是为了向后兼容性增加,使旧接口可用于利用JAVA8. lambda表达式的能力,例如,列表或集合接口不具备forEach方法声明.从而增加了这样的方 ...
- php开发环境是什么
软件开发环境(Software Development Environment,SDE)是指在基本硬件和数字软件的基础上,为支持系统软件和应用软件的工程化开发和维护而使用的一组软件,简称SDE.它由软 ...
- 如何配置JedisPool的参数
转自:http://blog.csdn.net/huahuagongzi99999/article/details/13631579 如何配置Pool的参数 JedisPool的配置参数很大程度上依赖 ...