题目如下:

Python代码:

# -*- coding:utf-8 -*-
def ismatch(s,p):
#先将dp[s+1][p+1]二维数组全置为False
dp = [[False] * (len(s) + 1) for _ in range(len(p)+1)]
dp[0][0] = True
for i in range(1,len(p)):
dp[i+1][0] = dp[i-1][0] and p[i] == '*'
for i in range(len(p)):
for j in range(len(s)):
if p[i]=='*':
# or运算相当于并,and相当于交
dp[i+1][j+1] = dp[i-1][j+1] or dp[i][j+1]
if p[i-1] == s[j] or p[i-1] == '.':
# |=相当于并,&=相当于交
dp[i+1][j+1] |= dp[i+1][j]
else:
dp[i+1][j+1] = dp[i][j] and (p[i] == s[j] or p[i] == '.')
return dp[-1][-1] print ismatch('aab','c*a*b*')

LeetCode(10)Regular Expression Matching的更多相关文章

  1. LeetCode(10) Regular Expression Matching

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

  2. Leetcode(10)正则表达式匹配

    Leetcode(10)正则表达式匹配 [题目表述]: 给定一个字符串 (s) 和一个字符模式 (p).实现支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符. '*' 匹配零个或 ...

  3. leetcode第十题--Regular Expression Matching

    Problem:Implement regular expression matching with support for '.' and '*'. '.' Matches any single c ...

  4. Leetcode(10)-正则表达式匹配

    给定一个字符串 (s) 和一个字符模式 (p).实现支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符. '*' 匹配零个或多个前面的元素. 匹配应该覆盖整个字符串 (s) ,而不 ...

  5. LeetCode(10):正则表达式匹配

    Hard! 题目描述: 给定一个字符串 (s) 和一个字符模式 (p).实现支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符. '*' 匹配零个或多个前面的元素. 匹配应该覆盖整 ...

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

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

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

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

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

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

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

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

随机推荐

  1. Linux apache tomcat

    [root@node1 ~]# mv jdk-7u79-linux-x64.tar.gz /usr/local/[root@node1 ~]# cd /usr/local/[root@node1 lo ...

  2. TF基础2

    1.常用API 1.图,操作和张量 tf.Graph,tf.Operation,tf.Tensor 2.可视化 TensorBoard 3.变量作用域 在TF中有两个作用域(scope),一个是nam ...

  3. io框架

    IO流的三种分类方式 1.按流的方向分为:输入流和输出流 2.按流的数据单位不同分为:字节流和字符流 3.按流的功能不同分为:节点流和处理流 (节点流表示的是直接操作节点(例如文件,键盘)的流,例如F ...

  4. Servlet的生命周期和Jsp的生命周期

    Servlet的生命周期: 1)构造方法(第1次访问) 2)init方法(第1次访问) 3)service方法 4)destroy方法 Jsp的生命周期 1)翻译: jsp->java文件 2) ...

  5. [转] hadoop MapReduce实例解析-非常不错,讲解清晰

    来源:http://blog.csdn.net/liuxiaochen123/article/details/8786715?utm_source=tuicool 2013-04-11 10:15 4 ...

  6. make 编译 linux 内核是单线程的任务 才用-j4命令使用4 线程加速

    今天使用 make 编译 linux 内核,发现CPU只用了30%多一点,而我的电脑是4核的,所以如果没有意外的话,make 编译 linux 内核的任务是用单线程做的. 又了解到,使用-j4参数可以 ...

  7. HDU 1575 Tr A( 简单矩阵快速幂 )

    链接:传送门 思路:简单矩阵快速幂,算完 A^k 后再求一遍主对角线上的和取个模 /********************************************************** ...

  8. [BZOJ3438][洛谷P1361]小M的作物

    题目大意:有A.B两个集合和n个物品,每个物品只能放在一个集合里.每个物品放在不同集合内能获得不同价值.有一些物品,如果它们同时放在一个集合内,则会产生新的价值(A和B中都有且不一定相同(c1和c2) ...

  9. java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

    缺少slf4j的包: 添加依赖: 代码: 1 <dependency> 2 <groupId>org.slf4j</groupId> 3 <artifactI ...

  10. apche本地测试,无法访问此网站