# -*- 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===
自我感觉很解得很垃圾啊。如果不是把p做了压缩还过不了online judge。其实就是一个DFA。 当s和p都是空的时候,匹配成功。
如果s为空,p不为空,检查p的偶数为是不是都是*。 如果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 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 match_char = p[0]
multi = False
if len(p) > 1:
if p[1] == "*":
multi = True if match_char != s[0] and match_char != ".":
if multi:
return isMatch2(s, p[2:])
else:
return False 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 return isMatch2(s, compress(p)) def main():
s = Solution()
print s.isMatch("aa", "a*") 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. HTTP中的URL长度限制(资料整理)

    HTTP中的URL长度限制   首先,其实http 1.1 协议中对url的长度是不受限制的,协议原文: The HTTP protocol does not place any a priori l ...

  2. Python爬虫实战(1):爬取Drupal论坛帖子列表

    1,引言 在<Python即时网络爬虫项目: 内容提取器的定义>一文我们定义了一个通用的python网络爬虫类,期望通过这个项目节省程序员一半以上的时间.本文将用一个实例讲解怎样使用这个爬 ...

  3. python image show()方法的预览问题

      在windows下面使用PIL中Image的show()函数时,执行下列代码: from PIL import Image img = Image.open("1.png") ...

  4. NET Core 介绍

    NET Core 介绍 标签: ASP.NETCore 1. 前言 2. ASP.NET Core 简介 2.1 什么是ASP.NET Core 2.2 ASP.NET Core的特点 2.3 ASP ...

  5. 移动网络山寨版(OpenBTS)【2】频段的故事

    OpenBTS 系统有两个看点.一个是无线收发,尤其是频段的处理,另一个是网络系统,尤其是替代传统的基站(BTS),基站控制器(BSC),移动控制中心(MSC),以及(HLR/VLR)的另类方案. 先 ...

  6. 中国25位最具影响力的IC人物

    当今许多企业的领导者几乎已经成为其企业的代名词,而在芯片业,我们听说的更多的是“龙芯”.“国芯”,他们的领导者的声音却鲜有传出.芯片业透出的强烈的民族色彩,也使这些隐身的企业家们的注意力更聚集在研发上 ...

  7. chrome提供的功能

    chrome://chrome-urls/ List of Chrome URLs chrome://accessibility chrome://appcache-internals chrome: ...

  8. 高效 Java Web 开发框架 JessMA v3.2.3 beta-2 发布

    JessMA(原名:Portal-Basic)是一套功能完备的高性能 Full-Stack Web 应用开发框架,内置可扩展的 MVC Web 基础架构和 DAO 数据库访问组件(内部已提供了 Hib ...

  9. 公司需求知识自学- Hashtable简单应用

    定义表 Hashtable tblNames = new Hashtable(); 添加key,value键值对 tblNames.Add("A","a"); ...

  10. linux学习之八-Linux本机性能监控

    Linux 优化 1.看看磁盘有没有满(根目录有没有满).内存有没有满.CPU有没有满 查看磁盘有没有满,使用df -h  看看磁盘使用情况 查看内存使用  free -m 特别关注swap用了多少 ...