[LeetCode]题解(python):010-Regular Expression Matching
题目来源:
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的更多相关文章
- [Leetcode][Python][DP]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- 【LeetCode】010. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- LeetCode 010 Regular Expression Matching
题目描述:Regular Expression Matching Implement regular expression matching with support for '.' and '*' ...
- 010 Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character. ...
- No.010 Regular Expression Matching
10. Regular Expression Matching Total Accepted: 89193 Total Submissions: 395441 Difficulty: Hard Imp ...
- leetcode第十题--Regular Expression Matching
Problem:Implement regular expression matching with support for '.' and '*'. '.' Matches any single c ...
- LeetCode--No.010 Regular Expression Matching
10. Regular Expression Matching Total Accepted: 89193 Total Submissions: 395441 Difficulty: Hard Imp ...
- LeetCode(10) Regular Expression Matching
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
- LeetCode(10)Regular Expression Matching
题目如下: Python代码: # -*- coding:utf-8 -*- def ismatch(s,p): #先将dp[s+1][p+1]二维数组全置为False dp = [[False] * ...
随机推荐
- 一个利用Dataflow实现的Actor
最近进行并发数据处理,学习到了 Actor模型,其中最简单的实现方式是一位大牛利用Dataflow实现的. 大牛的方案:http://www.jayway.com/2013/11/15/an-acto ...
- 学习java的视频资源(尚学堂)(比较老旧,但是还是挺好用)
本人新手,转入IT,一开始在学校的时候看过尚学堂 马士兵讲过的java基础视频教程,这次深入学习呢,就从百度云盘找了一整套的视频资源.之后越深入的学习呢,发现这些视频资源VeryCD上都发布了,地址 ...
- <转>Java的一些历史
Java是一种固执己见的语言,它具有很好的可读性,初级程序员很容易上手,具有长期稳定性和可支持性.但这些设计决定也付出了一定的代价:冗长的代码,类型系统与其它语言相比显得缺乏弹性. 然而,Java ...
- [Linked List]Delete Node in a Linked List
otal Accepted: 48115 Total Submissions: 109291 Difficulty: Easy Write a function to delete a node (e ...
- [Linked List]Remove Nth Node From End of List
Total Accepted: 84303 Total Submissions: 302714 Difficulty: Easy Given a linked list, remove the nth ...
- sql的集合运算
表的加减法 union:使用union 对表进行假发(并集)运算, union等集合运算符通常都会去除重复记录. select shohin_id, shohin_mei from shohin un ...
- Android Intent的花样启动
刚开始看郭大神的<>,实现以下里面的一些例子.Intent的花样启动 显示Intent的使用. 实例化一个Intent,并且制定当前的activity和要跳转到的activity Inte ...
- JQuery给元素绑定click事件多次执行的解决方法
原绑定方法: $(".subNavdiv").click(function () { ###### }); 这种方法只会在原click方法中继续添加新方法: 解决办法更改绑定方法为 ...
- HTML5新元素
<figure> 标签规定独立的流内容(图像.图表.照片.代码等等). <figure> 元素的内容应该与主内容相关,同时元素的位置相对于主内容是独立的.如果被删除,则不应对文 ...
- 用正则表达式抓取网页中的ul 和 li标签中最终的值!
获取你要抓取的页面 const string URL = "http://www.hn3ddf.gov.cn/price/GetList.html?pageno=1& ...