Regular Expression Matching

看到正则就感觉头大,因为正则用好了就很强大。有挑战的才有意思。

其实没有一点思路。循环的话,不能一一对比,匹配模式解释的是之前的字符。那就先遍历模式把。

... 中间 n 次失败的提交

感觉代码逻辑很乱。重新捋一下再动手写。

找几个重点分析一下:

Wrong Answer:

Input:
"aaa"
"ab*a*c*a"
Output:
false
Expected:
true

调试

aaa ab*a*c*a
0 a a s
1 a b n
1 a * * b
1 a a s
2 a * * a
prev char eq
False

分析,aaa字符串s中s[1]的a被模式p[3]中的a匹配了,然后s[2]的a被p[4]的*匹配了。还是没有解决*匹配0次的问题,那就得预先判断后面是啥模式而不是在之后判断前面的一个模式是啥。

N小时后来更,改了好多次没有解决匹配0-多次字符之后还有该字符。

可能我钻牛角尖了,删掉重新想一种思路。

... 又 n 次失败的本地测试
failed submission
import time

class Solution:
def __init__(self):
self.any='.' # any character
self.zom='*' # zero or more
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
ci=0 prevPattern=None for pi,pa in enumerate(p):
if ci==len(s):
if len(s)==0:
continue if ci>0 and prevPattern==self.zom:
ci-=1
else:
break
print("other:",pa)
#continue
while ci < len(s):
ci+=1
print(pi,pa,ci-1,s[ci-1],end="| ")
if pa==self.any:
print('.')
break
elif pa==self.zom:
print('*',prevPattern)
if prevPattern==self.any:
continue
elif prevPattern==s[ci-1]:
continue
else:
# no match, end processing
pass
#prevPattern=''
ci-=1
break
break
elif pa==s[ci-1]:
# same character
print('s')
break
else:
print('n')
ci-=1
break
prevPattern=pa
else:
return ci==len(s) return False if __name__ == "__main__": data = [
{
"input":{'s':'aa','p':'a'},
"output":False,
},
{
"input":{'s':'aa','p':'a*'},
"output":True,
},
{
"input":{'s':'ab','p':'.*'},
"output":True,
},
{
"input":{'s':'aab','p':'c*a*b'},
"output":True,
},
{
"input":{'s':'mississippi','p':'mis*is*p*.'},
"output":False,
},
{
"input":{'s':'aaa','p':'ab*a*c*a'},
"output":True,
},
{
"input":{'s':'ab','p':'.*c'},
"output":False,
},
{
"input":{'s':'axb','p':'a.b'},
"output":True,
},
{
"input":{'s':'mississippi','p':'mis*is*ip*.'},
"output":True,
},
{
"input":{'s':'aaa','p':'a*a'},
"output":True,
},
{
"input":{'s':'','p':'.*'},
"output":True,
},
{
"input":{'s':'aaa','p':'aaaa'},
"output":False,
},
{
"input":{'s':'a','p':'ab*'},
"output":True,
}
];
for d in data: print(d['input']['s'],d['input']['p']) # 计算运行时间
start = time.perf_counter()
result=Solution().isMatch(d['input']['s'],d['input']['p'])
end = time.perf_counter() print(result)
if result==d['output']:
print("--- ok ---",end="\t")
else:
raise Exception print(start-end)

不行,今天大半天都浪费到这上面了,怀疑人生。

去搜索一下,发现:

总结:想法本来就没有成熟,之前的题目都是一些常规的,这个正则不研究没有理论支撑可不好用。

等日后再战

LeetCode 失败的尝试 10. regular expression matching & 正则的更多相关文章

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

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

  2. Leetcode 10. Regular Expression Matching(递归,dp)

    10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...

  3. leetcode 10. Regular Expression Matching 、44. Wildcard Matching

    10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...

  4. 刷题10. Regular Expression Matching

    一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...

  5. LeetCode (10): Regular Expression Matching [HARD]

    https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...

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

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

  7. leetcode problem 10 Regular Expression Matching(动态规划)

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

  8. 【一天一道LeetCode】#10. Regular Expression Matching

    一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...

  9. 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]

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

随机推荐

  1. openLayer3地图的使用心得

    准备运行环境: 1)Portable Basemap Server(PBS)用于创建地图服务 官网网址:http://geopbs.codeplex.com/ 如何创建底图服务?操作步骤如下: 如果启 ...

  2. eclipse中maven运行run as clean等没反应处理方式

    在jdk配置处添加参数: -Dmaven.multiModuleProjectDirectory=$MAVEN_HOME 注意:这里有一个前提就是你已经正确安装maven [在环境变量中添加MAVEN ...

  3. How to set up github to work with Visual Studio 2013

    http://michaelcrump.net/setting-up-github-to-work-with-visual-studio-2013-step-by-step/ 1. Create gi ...

  4. VUE简单组件通信

    [x] 1.prop组件通信 1.简单理解 2.多层嵌套 [x] 2.使用ref进行组件通信 [x] 3.$emit组件通信 1.prop组件通信 1.简单理解 有点类似于应式的感觉,我不管你要不要只 ...

  5. python 中的 metaclass

    最遇到一个问题. class Meta(type): pass class M1(Meta): pass class M2(metaclass=M1): pass class Test(M2,meta ...

  6. ToString()、Convert.ToString()、(string)、as string 的区别

    通常 object 到 string 有四种方式(假设有object obj):obj.ToString().Convert.ToString().(string)obj.obj as string. ...

  7. 解决js输出汉字乱码问题

    当我们需要使用js输出汉字时,偶然会出现输出的中文汉字乱码的情况,在网上收了很多解决方案 1.在mata中加 <meta content="text/html; charset=utf ...

  8. tomcat源码 StandardServer

    在执行org.apache.catalina.startup.Catalina#load的时候会执行org.apache.catalina.core.StandardServer#init,然后会调到 ...

  9. vagrant 本地添加box 支持带版本号

    众所周知,vagrant添加box的时候要从外网下载,那速度...(说多了都是泪),所以只好用下载工具下载到本地之后再添加. 一般处理方案 vagrant box add boxName ./down ...

  10. MOngoDB为现有数据添加或删除某一字段

    var lst =db.getCollection('config').find({}); while(lst.hasNext()) { var site=lst.next(); db.config. ...