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===
    自我感觉很解得很垃圾啊。如果不是把p做了压缩还过不了online judge。其实就是一个DFA。
  14.  
  15. 当s和p都是空的时候,匹配成功。
    如果s为空,p不为空,检查p的偶数为是不是都是*。
  16.  
  17. 如果s和p都不为空,头一个字母
    如果不匹配,
    不带*,False
    带*,递归p[2:]
    如果匹配,
    不带*,递归s[1:],p[1:]
    带*,考虑匹配0到最远端的情况,分别递归s[i:],p[2:]
    '''
    class Solution:
    # @return a boolean
    def isMatch(self, s, p):
    def compress(p):
    i = 0
    while i < len(p)-3:
    if p[i+1] != '*':
    i = i + 1
    continue
    if p[i+3] == '*':
    if p[i] == "." or p[i+2] == ".":
    p = p[:i] + ".*" + p[i+4:]
    continue
    elif p[i] == p[i+2]:
    p = p[:i] + p[i+2:]
    continue
    i = i + 2
    return p
  18.  
  19. def isMatch2(s, p):
    if len(s) == 0:
    if len(p) == 0:
    return True
    if len(p) % 2 == 0:
    i = 1
    while i < len(p):
    if p[i] != "*":
    return False
    i = i + 2
    else:
    return True
    else:
    return False
    if len(s) > 0 and len(p) == 0:
    return False
  20.  
  21. match_char = p[0]
    multi = False
    if len(p) > 1:
    if p[1] == "*":
    multi = True
  22.  
  23. if match_char != s[0] and match_char != ".":
    if multi:
    return isMatch2(s, p[2:])
    else:
    return False
  24.  
  25. if multi is False:
    return isMatch2(s[1:], p[1:])
    else:
    result = False
    i = 0
    result = isMatch2(s, p[2:])
    while i < len(s):
    if result == True:
    return result
    if (s[i] == match_char or match_char == "."):
    result = isMatch2(s[i+1:], p[2:])
    else:
    break
    i = i + 1
    return result
  26.  
  27. return isMatch2(s, compress(p))
  28.  
  29. def main():
    s = Solution()
    print s.isMatch("aa", "a*")
  30.  
  31. if __name__ == "__main__":
    import time
    start = time.clock()
    main()
    print "%s sec" % (time.clock() - start)

[LeetCode][Python]Regular Expression Matching的更多相关文章

  1. leetcode 10 Regular Expression Matching(简单正则表达式匹配)

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

  2. LeetCode (10): Regular Expression Matching [HARD]

    https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...

  3. 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]

    题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...

  4. [LeetCode] 10. Regular Expression Matching 正则表达式匹配

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

  5. Leetcode 10. Regular Expression Matching(递归,dp)

    10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...

  6. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

  7. 【leetcode】Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  8. 【leetcode】Regular Expression Matching (hard) ★

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

  9. 【JAVA、C++】LeetCode 010 Regular Expression Matching

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

随机推荐

  1. 【原】从一个bug浅谈YUI3组件的资源加载

    篇前声明:为了不涉及业务细节,篇内信息统一以某游戏,某功能代替 前不久,某游戏准备内测客户端,开发人员测试过程中发现某功能突然不灵了,之前的测试一切ok,没有发现任何异常,第一反应是,游戏内浏览器都是 ...

  2. Javascript 多物体运动——逐行分析代码,让你轻松了解运动的原理

    我们先来看下之前的运动的代码,是否支持多物体运动,会出现怎么样的问题. <style type="text/css"> div { width: 100px; heig ...

  3. php 截取字符串

    /** * 方法库-截取字符串-[该函数作者未知] * @param string $string 字符串 * @param int $length 字符长度 * @param string $dot ...

  4. c语言字符串翻转系列

    2013-10-25 最近碰到一道笔试题,是关于字符串翻转的.题目是:将一段英文翻转,但保留单词拼写,如给定字符串str="I am a student",返回为"stu ...

  5. 确定比赛名次(map+邻接表 邻接表 拓扑结构 队列+邻接表)

    确定比赛名次 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  6. poj 2686 Traveling by Stagecoach ---状态压缩DP

    题意:给出一个简单带权无向图和起止点,以及若干张马车车票,每张车票可以雇到相应数量的马. 点 u, v 间有边时,从 u 到 v 或从 v 到 u 必须用且仅用一张车票,花费的时间为 w(u, v) ...

  7. UIViewController、UINavigationController与UITabBarController的整合使用

    UINavigationController与UITabBarController是iOS开发中最常用的两种视图控制器,它们都属于UIViewController的子类,继承关系如下: @interf ...

  8. mysql distinct&group by 应用

    在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的 ...

  9. js charts去掉logo

    打开js charts 3的源代码搜索关键字"fs.bg",然后会找到 fs.bg.2v(fX),将这句代码删掉就OK了,可能有的版本会是fs.bg.2u(fX) 欢迎加入群:25 ...

  10. @Html.ValidationSummary()的使用

    @Html.ValidationSummary()用于返回表单在后台验证的结果. 如, 当后台if (ModelState.IsValid)失败后,错误信息就会显示到 @Html.Validation ...