[leetcode]Regular Expression Matching @ Python
原题地址:https://oj.leetcode.com/problems/regular-expression-matching/
题意:
Implement regular expression matching with support for '.'
and '*'
.
'.' Matches any single character.
'*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be:
bool isMatch(const char *s, const char *p) Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true
解题思路:正则表达式匹配的判断。网上很多的解法是用递归做的,用java和c++都可以过,但同样用python就TLE,说明这道题其实考察的不是递归。而是动态规划,使用动态规划就可以AC了。这里的'*'号表示重复前面的字符,注意是可以重复0次的。
先来看递归的解法:
如果P[j+1]!='*',S[i] == P[j]=>匹配下一位(i+1, j+1),S[i]!=P[j]=>匹配失败;
如果P[j+1]=='*',S[i]==P[j]=>匹配下一位(i+1, j+2)或者(i, j+2),S[i]!=P[j]=>匹配下一位(i,j+2)。
匹配成功的条件为S[i]=='\0' && P[j]=='\0'。
代码,TLE:
class Solution:
# @return a boolean
def isMatch(self, s, p):
if len(p)==0: return len(s)==0
if len(p)==1 or p[1]!='*':
if len(s)==0 or (s[0]!=p[0] and p[0]!='.'):
return False
return self.isMatch(s[1:],p[1:])
else:
i=-1; length=len(s)
while i<length and (i<0 or p[0]=='.' or p[0]==s[i]):
if self.isMatch(s[i+1:],p[2:]): return True
i+=1
return False
再来看动态规划的解法。
代码:
class Solution:
# @return a boolean
def isMatch(self, s, p):
dp=[[False for i in range(len(p)+1)] for j in range(len(s)+1)]
dp[0][0]=True
for i in range(1,len(p)+1):
if p[i-1]=='*':
if i>=2:
dp[0][i]=dp[0][i-2]
for i in range(1,len(s)+1):
for j in range(1,len(p)+1):
if p[j-1]=='.':
dp[i][j]=dp[i-1][j-1]
elif p[j-1]=='*':
dp[i][j]=dp[i][j-1] or dp[i][j-2] or (dp[i-1][j] and (s[i-1]==p[j-2] or p[j-2]=='.'))
else:
dp[i][j]=dp[i-1][j-1] and s[i-1]==p[j-1]
return dp[len(s)][len(p)]
[leetcode]Regular Expression Matching @ Python的更多相关文章
- [LeetCode] Regular Expression Matching 正则表达式匹配
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] Regular Expression Matching(递归)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [LeetCode] Regular Expression Matching [6]
称号: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- LeetCode——Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- Leetcode:Regular Expression Matching分析和实现
题目大意是要求我们实现一个简单的正则表达式全匹配判断.其中正则表达式中只包含一般字符,以及全匹配字符.和变长字符*.其中.可以匹配一个字符,而*与前一个字符相关联,x*可以被看作任意多个x(0到正无穷 ...
- LeetCode Regular Expression Matching 网上一个不错的实现(非递归)
'.' Matches any single character.'*' Matches zero or more of the preceding element. The matching sho ...
- LeetCode: Regular Expression Matching 解题报告
Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...
- lc面试准备:Regular Expression Matching
1 题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...
随机推荐
- [ 转载 ] Java中成员变量 和局部变量
java语言支持的变量类型 类变量:独立于方法之外的变量,用 static 修饰. 局部变量:类的方法中的变量. 实例变量(全局变量):独立于方法之外的变量,不过没有 static 修饰. publi ...
- netbeans启动后一会崩溃处理
由于netbeans 默认不支持amd cpu渲染,故需要修改默认配置文件,修改后netbeans没有问题. http://stackoverflow.com/questions/34560485/n ...
- 【BZOJ 1430】 1430: 小猴打架 (Prufer数列)
1430: 小猴打架 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 625 Solved: 452 Description 一开始森林里面有N只互不相 ...
- android shareUID
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 安卓中 全局进程,其他应用可以通过 分享UID 方式 和它 在同一个进程中.
- HDU 4498 Function Curve (自适应simpson)
Function Curve Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)To ...
- Extended APDU support
http://pcsclite.alioth.debian.org/ccid_extended_apdu.html To be able to use an extended APDU you nee ...
- InnoDB 与 MYISAM
http://www.cnblogs.com/sopc-mc/archive/2011/11/01/2232212.html
- PHP 7 来了,PHP 6 去哪儿了?
PHP7来了,那么PHP6去哪儿了呢? PHP7简介 PHP7是PHP编程语言全新的一个版本,主要在性能方面获得了极大的提升.官方的文档显示,PHP7可以达到PHP5.x版本两倍的性能.同时还 对PH ...
- 移植Python3到TQ2440(二)
接着前一篇博文. 在上一篇博文中我们用NFS挂载根文件系统的方式启动了系统,接下来我们把移植了Python3的根文件系统固化到NandFlash中,但是由于linux-4.9目前不支持Yaffs2文件 ...
- Task.Delay方法的2个应用实例,单元测试等待,限时限次下载远程资源
如果想让程序异步等待一段时间,可以考虑使用Task.Delay方法. 比如,在单元测试中模拟一个异步操作. static async Task<T> DelayedResult<T& ...