[Leetcode][Python][DP]Regular Expression Matching
# -*- coding: utf8 -*-
'''
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 ===Comments by Dabay===
这道题应该用动态规划的做法。我只想说,头大! 建立一个二维数组,第一维是s为基础,第二维是p为基础。(顺序很重要)
初始化res[0],长度比p多1,即多第一个true。同时考虑p中开始的几个偶数为是*的情况,初始化相应的true。其他为false。 然后每次放一个s中元素进来,尝试匹配整个p。 三种情况:(每次更新的是res[i+1][j+1])
如果p[j]不是.和*,即是一般比较,同时观察res[i][j]。
如果p[j]是.,无需比较,根据res[i][j]的值即可。
如果p[j]是*,
考虑匹配零次的情况:判断res[i+1][j-1]是否为true
考虑匹配一次的情况:判断res[i+1][j]是否为true
考虑匹配多次的情况:判断字符是否相符,同时判断res[i][j+1]
'''
class Solution:
# @return a boolean
def isMatch(self, s, p):
res = []
defaultRow = [False] + [False for _ in p]
res.append(list(defaultRow))
res[0][0] = True
for x in xrange(len(p)):
if p[x] == '*':
res[0][x+1] = res[0][x-1] for i in xrange(len(s)):
res.append(list(defaultRow))
for j in xrange(len(p)):
if p[j] != '*':
if (s[i] == p[j] or p[j] == '.') and res[i][j]:
res[i+1][j+1] = True
elif p[j] == '.':
if res[i][j]:
res[i+1][j+1] = True
else: # *
#匹配0次
if res[i+1][j-1]:
res[i+1][j+1] = True
#匹配1次
if res[i+1][j]:
res[i+1][j+1] = True
#匹配多次
if (s[i] == p[j-1] or p[j-1] == '.') and res[i][j+1]:
res[i+1][j+1] = True
return res[-1][-1] def main():
sol = Solution()
s = "aab"
p = "c*a*b"
print sol.isMatch(s, p) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[Leetcode][Python][DP]Regular Expression Matching的更多相关文章
- 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode problem 10 Regular Expression Matching(动态规划)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【一天一道LeetCode】#10. Regular Expression Matching
一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...
- LeetCode解题报告—— Regular Expression Matching
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- 【LeetCode】010. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【leetcode】10.Regular Expression Matching
题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- LeetCode OJ:Regular Expression Matching(正则表达式匹配)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【LeetCode】10.Regular Expression Matching(dp)
[题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...
- [leetcode]Regular Expression Matching @ Python
原题地址:https://oj.leetcode.com/problems/regular-expression-matching/ 题意: Implement regular expression ...
随机推荐
- BroadcastReceiver浅析
1.什么是BroadcastReceiver? 本质上是属于一个监听器,但onXxxListenter只是程序级别的监听器,当程序退出时候监听器也随之关闭.而BroadcastReceiver是系统级 ...
- C# 异步Socket
C# 异步Socket (BeginXXXX)服务器代码 前言: 1.最近维护公司的一个旧项目,是Socket通讯的,主要用于接收IPC(客户端)发送上来的抓拍图像,期间要保持通讯,监测数据包并进行处 ...
- 您在基于 Windows 7 的或基于 Windows Server 2008 R2 的计算机上读取器中插入智能卡时出现错误消息:"设备驱动程序软件未能成功安装"
http://support.microsoft.com/kb/976832/zh-cn http://support.microsoft.com/kb/976832/zh-tw 症状 当智能卡插入智 ...
- Qt 中程序自动重启
参照至 dbzhang老师的博文,记录于此....... 要想理解重启,先得看看Qt程序怎么退出! 1.退出 int main(int argc, char** argv) { QApplicatio ...
- Delphi 实现无窗口移动(发WM_NCHITTEST消息计算,然后再发WM_SYSCOMMAND消息,带参数SC_DRAGMOVE)
procedure imgListMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer) ...
- php中strstr、strrchr、substr、stristr四个函数用法区别
php中strstr.strrchr.substr.stristr四个函数用法区别: php中strstr strrchr substr stristr这四个字符串操作函数特别让人容易混淆,常用的是s ...
- linux 进程(一)---基本概念
一.进程的定义 进程是操作系统的概念,每当我们执行一个程序时,对于操作系统来讲就创建了一个进程,在这个过程中,伴随着资源的分配和释放.可以认为进程是一个程序的一次执行过程. 二.进 ...
- Formiko总结整数十进制转换二进制原理
引子: 为什么十进制转二进制的“辗转相除记录余数倒序输出”的算法是正确的?这个问题陪伴了Formiko半年. 实践: 实践一:把十进制数100转换成二进制数的图 上图和和下图唯一的区别在最后一位上 ...
- Android系统移植与调试之------->如何修改Android设备的开机第一阶段Logo
1.切换到~/mx0831-0525/device/other/TBDG1073/res_pack目录下 2.更换bootup和poweron文件 找一张bmp16位的图片去除后缀名将这两张都替换,转 ...
- OAuth2 for asp.net web api
在上篇文章中我研究了OpenId及DotNetOpenAuth的相关应用,这一篇继续研究OAuth2. https://github.com/DotNetOpenAuth http://www.cnb ...