题目来源:

https://leetcode.com/problems/regular-expression-matching/


题意分析:

这道题目定义了两个正则表达式规则。’.’代表任意字符,’*’代表前一个字符出现任意次。输入两个字符串s,p。如果s可以被p完全匹配则返回True,否则返回False。比如’.*’可以匹配任意字符串。


题目思路:

这道题目如果直接import 正则表达式肯定是不行的,因为题目只定义了2个特殊字符,而正则表达式包括很多特殊字符,所以直接import正则表达式肯定是不行的

从第一个字符开始搜索,对以下情况进行讨论:

①如果(s[0] == p[0]or p[0] == ‘.’)且p的第二个字符不是’*’,那么如果两个字符同时去掉第一个字符,结果不变。

②如果p[0] == ‘.’且p[1] == ‘*’那么如果存在s[i:],i >= 0被p[2:]完全匹配,则s可以被p完全匹配(s[i:]代表s字符除掉0到i下标的字符)。

③如果p[0] != ‘.’且p[1] == ‘*’,那么如果s[0] != p[0]那么如果s可以被p[2:]匹配,则s可以被p完全匹配。

④如果s[0] == p[0]那么如果存在s[i:](i >= 0 且 s[j] == p[0] 0<= j <i)被p[2:]完全匹配,那么s可以被p完全匹配。

经过上面的分析,这道题目可以用动态规划来做。用b[i][j]来代表由s除掉0到i下标的字符能否被由p除掉0到j下标的字符完全匹配,也就是s[i:]能否被p[j:]完全匹配。根据上一段的分析可以得到:

if p[j + 1] != ‘*’

if s[i] == p[j] or p[j] == ‘.’ then b[i][j] = b[i + 1][j + 1]

fi

else

if p[j] == ‘.’ then

if exist k (i<=k) b[k][j + 2] = true then b[i][j] = true

else b[i][j] = false

fi

else

if exist k(i <= k)b[k][j + 2] = true and all l (0 <= l <k) s[l] = p[0] then b[i][j] =true

else b[i][j] = false

fi

fi

return b[0][0]

PS:由于python的二维数组初始化比较麻烦,或者是因为我对python的不熟,我选择用dictionary来处理,b[i,j]代替b[i][j].


代码(python):

 class Solution(object):
def isMatch(self,s, p):
size1 = len(s)
size2 = len(p)
b = {}
s1 = 0
while s1 <= size1:
s2 = 0
while s2 <= size2:
b[s1,s2] = False
s2 += 1
s1 += 1
b[size1,size2] = True
j = size2 - 1
while j >= 0:
if p[j] == '*':
b[size1,j] = False
j -= 1
b[size1,j] = True
j -= 1
else:
break
j = size2 - 1
while j >= 0:
if p[j] == '.':
i = size1 - 1
while i >= 0:
b[i,j] = b[i + 1,j + 1]
i -= 1
elif p[j] != '*':
i = size1 - 1
while i >= 0:
if p[j] == s[i]:
b[i,j] = b[i + 1,j + 1]
else:
b[i,j] = False
i -= 1
else:
i = size1 - 1
if p[j - 1] == '.':
while i >= 0:
k = i
while k <= size1:
if b[k,j + 1]:
b[i,j - 1] =True
break
k += 1
i -= 1
else:
while i >= 0:
k = i
while k <= size1:
if b[k,j + 1]:
b[i,j - 1] = True
break
if k == size1:
break
if p[j - 1] != s[k]:
break
k +=1
i -= 1
j -= 1
j -= 1
return b[0,0]

转载请注明出处:http://www.cnblogs.com/chruny/p/4807234.html

[LeetCode]题解(python):010-Regular Expression Matching的更多相关文章

  1. [Leetcode][Python][DP]Regular Expression Matching

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...

  2. 【LeetCode】010. Regular Expression Matching

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

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

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

  4. LeetCode 010 Regular Expression Matching

    题目描述:Regular Expression Matching Implement regular expression matching with support for '.' and '*' ...

  5. 010 Regular Expression Matching 正则表达式匹配

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

  6. No.010 Regular Expression Matching

    10. Regular Expression Matching Total Accepted: 89193 Total Submissions: 395441 Difficulty: Hard Imp ...

  7. leetcode第十题--Regular Expression Matching

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

  8. LeetCode--No.010 Regular Expression Matching

    10. Regular Expression Matching Total Accepted: 89193 Total Submissions: 395441 Difficulty: Hard Imp ...

  9. LeetCode(10) Regular Expression Matching

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

  10. LeetCode(10)Regular Expression Matching

    题目如下: Python代码: # -*- coding:utf-8 -*- def ismatch(s,p): #先将dp[s+1][p+1]二维数组全置为False dp = [[False] * ...

随机推荐

  1. 卸载cloudera manager

    卸载Cloudera-Manager sudo /usr/share/cmf/uninstall-cloudera-manager.sh 一直选择Yes 就好 卸载完成后,它会问你是否要将databa ...

  2. 由chkconfig 引发的联想——怎么查看程序是否已经安装/成功安装

    由chkconfig 引发的联想--怎么查看程序是否已经安装/成功安装 某天需要运行chkconfig,root登录依然找不到该命令. [root@localhost ~]# chkconfig ba ...

  3. 使用XmlReader读Xml

    XmlDocument和XElement在读取Xml时要将整个Xml文档放到内存中去操作,这样做操作简单,但是很费内存和IO(可能是磁盘IO或者网络IO):而在有些场景下我们必须考虑尽可能节省内存和I ...

  4. synchronized 关键字

    synchronized 多用于并发不高并且需要单线程运行的地方.比如你有一个A方法,你在方法上加了synchronized修饰.那么两个人同时去调用这个方法的时候不是并行的,是抢占的,谁先抢到资源谁 ...

  5. STL_函数模板

    #include <iostream>#include <string>using namespace std; #define MAX(T) \ T max_##T (T x ...

  6. jsp中添加弹窗口并且实现向后台双向传递数据

    思路:使用jquery-easyui框架实现弹出div,在jsp初始化时从后台获得数据初始化div中的form表单元素的value属性,从而获得后台数据.在点击确定按钮时使用ajax向后台发送数据. ...

  7. js/jquery获取当前页面URL地址并判断URL字符串中是否包含某个具体值

    js/jquery获取当前页面URL地址并判断URL字符串中是否包含某个具体值本文介绍jquery/js获取当前页面url地址的方法,在jquery与js中获取当前页面url方法是一样的,因为jque ...

  8. 一元云购qq互联回调地址错误解决办法

    经过追踪,点击登录后调用 system/modules/api/下面的qqlogin.action.class.php 里面又调用了qq 互联php接口样例里的QC.php的QC类的方法qq_logi ...

  9. hdu 5446 Unknown Treasure 中国剩余定理+lucas

    题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. ...

  10. Codeforces 235E

    Codeforces 235E 原题 题目描述:设\(d(n)\)表示\(n\)的因子个数, 给定\(a, b, c\), 求: \[\sum_{i=1}^{a} \sum_{j=1}^{b} \su ...