# -*- 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的更多相关文章

  1. 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  2. leetcode problem 10 Regular Expression Matching(动态规划)

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  3. 【一天一道LeetCode】#10. Regular Expression Matching

    一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...

  4. LeetCode解题报告—— Regular Expression Matching

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  5. 【LeetCode】010. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  6. 【leetcode】10.Regular Expression Matching

    题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...

  7. LeetCode OJ:Regular Expression Matching(正则表达式匹配)

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  8. 【LeetCode】10.Regular Expression Matching(dp)

    [题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...

  9. [leetcode]Regular Expression Matching @ Python

    原题地址:https://oj.leetcode.com/problems/regular-expression-matching/ 题意: Implement regular expression ...

随机推荐

  1. WebApp开发技巧大全 看了就明白了

    [转载]阅读原文 自Iphone和Android这两个牛逼的手机操作系统发布以来,在互联网界从此就多了一个新的名词-WebApp(意为基于WEB形式的应用程 序,运行在高端的移动终端设备).开发者们都 ...

  2. [Python]从豆瓣电影批量获取看过这部电影的用户列表

    前言 由于之后要做一个实验,需要用到大量豆瓣用户的电影数据,因此想到了从豆瓣电影的“看过这部电影 的豆瓣成员”页面上来获取较为活跃的豆瓣电影用户. 链接分析 这是看过"模仿游戏"的 ...

  3. logstash 根据type 判断输出

    # 更多ELK资料请访问 http://devops.taobao.com 一.配置前需要注意: 1.Use chmod to modify nginx log file privilege. E.g ...

  4. Xlint以及Java Lint 选项

    Java Lint 选项 Java 编译器的选项包括所谓的标准选项和非标准选项.标准选项是指在当前版本的开发环境中支持,且在未来版本中也将被支持的选项.常用的标准选项比如 -classpath 以及 ...

  5. 海康威视研究院ImageNet2016竞赛经验分享

    原文链接:https://zhuanlan.zhihu.com/p/23249000 目录 场景分类 数据增强 数据增强对最后的识别性能和泛化能力都有着非常重要的作用.我们使用下面这些数据增强方法. ...

  6. 向未声明的 JavaScript 变量来分配值

    如果您把值赋给尚未声明的变量,该变量将被自动作为全局变量声明. 这条语句: carname="Volvo"; 将声明一个全局变量 carname,即使它在函数内执行.

  7. 使用VNC完毕远程调用图形化

    原创作品,出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明下面出处.否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlo ...

  8. Effective C++笔记 55条编程法则

    1.  视C++为一个语言联邦 C++高效编程守则视状况而变化,取决于你使用C++的哪一部分. 2.  尽量以const,enum.inline替代#define 1) 对于单纯常量,最好以const ...

  9. UITabBarController 笔记(二) ViewController中加UITabBarController

    新建一个简单视图iOS工程,在ViewController的viewDidLoad中代码如下 - (void)viewDidLoad { [super viewDidLoad]; // Do any ...

  10. 提交时提示错误This Bundle is invalid.New apps and app updates submitted to the App Store must be built wit

    this bundle is invalid . new apps and app updates submitted to the app store must be built with publ ...