1. # -*- coding: utf8 -*-
    '''
    https://oj.leetcode.com/problems/regular-expression-matching/
  2.  
  3. Implement regular expression matching with support for '.' and '*'.
  4.  
  5. '.' Matches any single character.
    '*' Matches zero or more of the preceding element.
  6.  
  7. The matching should cover the entire input string (not partial).
  8.  
  9. The function prototype should be:
    bool isMatch(const char *s, const char *p)
  10.  
  11. 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
  12.  
  13. ===Comments by Dabay===
    这道题应该用动态规划的做法。我只想说,头大!
  14.  
  15. 建立一个二维数组,第一维是s为基础,第二维是p为基础。(顺序很重要)
    初始化res[0],长度比p多1,即多第一个true。同时考虑p中开始的几个偶数为是*的情况,初始化相应的true。其他为false。
  16.  
  17. 然后每次放一个s中元素进来,尝试匹配整个p。
  18.  
  19. 三种情况:(每次更新的是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]
  20.  
  21. 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]
  22.  
  23. def main():
    sol = Solution()
    s = "aab"
    p = "c*a*b"
    print sol.isMatch(s, p)
  24.  
  25. 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. 这些年,我收集的JavaScript代码(一)

    一.取URL中的参数 function getParameterByName(name) { var match = RegExp('[?&]' + name + '=([^&]*)' ...

  2. jQuery验证控件(转载)

    转自:http://www.cnblogs.com/hejunrex/archive/2011/11/17/2252193.html 官网地址:http://bassistance.de/jquery ...

  3. jQuery学习-事件之绑定事件(五)

    大家应该还记得dispatch方法中有这么一段代码: event = jQuery.event.fix( event ); event的修复是在fix这个方法中的,而在fix中是通过 new jQue ...

  4. PMC

    PMC = Production Material Control 生产及物料控制,通常分为两个部分: PC:生产控制或生产管制(台.日资公司俗称手配)主要职能是生产的计划与生产的进度控制 : MC: ...

  5. MYSQLinsert速度过慢

    MYSQLinsert速度过慢最近在用MySQL做存储,测试中发现插入数据太慢了,插入速度只有20 MY SQL insert 速度过慢最近在用MySQL做存储,测试中发现插入数据太慢了,插入速度只有 ...

  6. poj3077---进位

    #include <stdio.h> #include <stdlib.h> #include<string.h> ]; ]; int main() { int n ...

  7. CSS滤镜让图片模糊(毛玻璃效果)实例页面

    <pre name="code" class="css">CSS代码: .blur { filter: url(blur.svg#blur); /* ...

  8. #include <limits.h>

    limits.h专门用于检测整型数据数据类型的表达值范围. #include<stdio.h> #include<limits.h> main() { printf(" ...

  9. css的绝对定位

    假设绝对定位的元素的id为absoluteDiv. 当包含absoluteDiv的块中没有设置position:relative时, absoluteDiv会相对于浏览器(window.top)定位. ...

  10. iOS7.0中UILabel高度调整注意事项(转)

    注释:原文链接丢失. 我的“记词助手”在升级到iOS7之后,一直出现UILabel错位的问题: 我的label是用- (CGSize)sizeWithFont:(UIFont *)font const ...