[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 ...
随机推荐
- Javascript 多物体运动——逐行分析代码,让你轻松了解运动的原理
我们先来看下之前的运动的代码,是否支持多物体运动,会出现怎么样的问题. <style type="text/css"> div { width: 100px; heig ...
- 1011. A+B和C
/* * Main.c * 1011. A+B和C * Created on: 2014年8月30日 * Author: Boomkeeper *********测试通过******* */ #inc ...
- 安装ngix
第一步:解压源码包 第二步:./configure -->这个时候会提示缺少PCRE 这个时候要安装yum -y install pcre-devel 第三步:./configure --> ...
- Linux--本地yum库
Linux配置本地yum云: 第一步:把cd 挂载到目录树 mount /dev/cdrom /mnt/media 第二步:增加/etc/yum.repo.d/local.repo touch /et ...
- bash快捷建-光标移到行首、行尾等
转自:http://digdeeply.org/archives/12131599.html ctrl键组合ctrl+a:光标移到行首.ctrl+b:光标左移一个字母ctrl+c:杀死当前进程.ctr ...
- WPS Office手机版调用接口代码指导帖之一(Android)
经常会有一些喜欢开发鼓捣的童鞋问我们,WPS Office手机版是否提供调用接口,希望在android中使用一个调用命令,直接调用WPS手机版来打开指定的DOC文件,而不用弹出一个程序可选列表(如果用 ...
- API 设计: RAML、Swagger、Blueprint三者的比较
API设计工具中常常会拿RAML.Swagger.Blueprint这三种工具进行讨论比较,它们都是用来描述和辅助API开发的,只是它们之间的侧重有所不同. RAML RAML(RESTful API ...
- 在 Windows Azure 网站中进行纵向扩展和横向扩展
编辑人员注释:本文章由 Windows Azure 网站团队的项目经理 Byron Tardif 撰写. 当您开始一个新的 Web 项目,或者刚刚开始开发一般的网站和应用程序时,您可能希望从小处着手. ...
- nexus 7 2013 驱动安装及root
驱动安装 Nexus 7 2013连接上电脑后,设备管理器显示新设备 nexus 7 待安装驱动(其实是MTP设备待安装驱动).去谷歌网站下载最新的USB驱动,version 8.0 的.与以前的版本 ...
- Jquery html页面处理基础
1.怎样获得浏览器的可视高度? var windHight = $(window).height(); //获得浏览器的可视高度 2.怎样获得滚动条相对于顶部的高度? var scrollHi ...