[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 ...
随机推荐
- php正则提取img所有属性值
$ext = 'gif|jpg|jpeg|bmp|png';//罗列图片后缀从而实现多扩展名匹配 by http://www.k686.com 绿色软件 $str = ''; $list = arra ...
- MVC in Javascript
MVC in Javascript From http://www.cnblogs.com/tugenhua0707/p/5156179.html 原博的比我详细 我是以自己的惯用的方式实现了一下 M ...
- QFN和QFP的区别
QFN(quad flat non-leaded package)四侧无引脚扁平封装.多称为LCC. 陶瓷QFN :基本上都是LCC 标记. 塑料QFN 也称为塑料LCC.PCLC.P-LCC 等. ...
- MAC OS X 快捷键(自己总结)
command+space 可以切换键盘输入法:长按可进入输入法列表,并在多个输入法之间切换,输入法列表会根据你最近使用过的输入法自动调整排序. HID:00 00 91 00 00 00 00 00 ...
- android PopupWindow显示位置
PopupWindow的显示及位置设置 window.showAtLocation(parent, Gravity.RIGHT | Gravity.BOTTOM, 10,10); 第一个参数指定Pop ...
- [技巧]使用Xcode集成的HeaderDoc自动生成注释和开发文档
[技巧]使用Xcode集成的HeaderDoc自动生成注释和开发文档 Doxygen本来是一个很好的工具,可是我感觉在mac系统下,如果用doxygen最后生成的CHM文件感觉就不是那么恰当, ...
- 负margin新解
第一篇 我知道你不知道的负Margin 分类: Html/CSS | 转载请注明: 出自 海玉的博客 本文地址: http://www.hicss.net/i-know-you-do-not-know ...
- C#中dynamic的正确用法【转】
dynamic是FrameWork4.0的新特性.dynamic的出现让C#具有了弱语言类型的特性.编译器在编译的时候不再对类型进行检查,编译期默认dynamic对象支持你想要的任何特性.比如,即使你 ...
- Sql Service存储过程分页
一起是用oracle数据库..感觉oracle数据库强大.查询速度是杠杠的.换了家公司用的是SQL SERVICE.以前用了1年现在捡回以前的记忆.动手写了动态SQL过存储过程分页.感觉和oracle ...
- ssh框架用JUnit测试
public class testAuxDict { //读spring配置文件 public static BeanFactory factory = new ClassPathXmlApplica ...