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. VB Byte数组转字符串问题

    在c#中,byte转换为字符串的过程中,如果byte的值为0,则转换成字符串时变为’\0’字符,’\0’字符在C#中意味着字符串结束,如果后面再有字符,则读取字符串的程序也不能读取和显示出来. 但是在 ...

  2. [转]You Could Become an AI Master Before You Know It. Here’s How.

    转自:https://www.technologyreview.com/s/608921/ai-algorithms-are-starting-to-teach-ai-algorithms/# You ...

  3. EXCEL 将网络图片地址插入为锁定的图片单元格宏

    Sub InsertPic2(ByVal 图片链接 As String, ByVal 插入图片表名 As String, ByVal 插入图片单元格地址 As String) On Error Res ...

  4. 《LOST》 电视

    还没看正剧,所以转来帮助看电视 从起源到终点:<LOST>剧情全解析(一)   此文是LOST完结之后的剧情解析,剧透,慎入   从起源到终点:<LOST>剧情全解析(一) 转 ...

  5. 阿里云ECS安装Kubernetes问题收集与解答

    问题1 kubernetes pod启动报错open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such fil ...

  6. 阿里云ECS专有网络下安装flannel注意事项

    参照文章http://www.cnblogs.com/lyzw/p/6016789.html在两台阿里云ECS主机安装与配置flannel,在专有网络下两台主机只能通过公网ip连通,所以flannel ...

  7. Dubbo(2)发布Dubbo服务

    主要参考Dubbo源码包里面的dubbo-demo源码: 1.项目结构: 2.pom.xml中的依赖: <project xmlns="http://maven.apache.org/ ...

  8. springboot工程添加404页面

    首先在/src/main/resources下创建文件夹/public/error 在文件夹里创建html页面,jsp页面不可以. <html> <body> <img ...

  9. Linux CentOS 下关闭防火墙

    永久关闭(重启后生效) 开启: chkconfig iptables on 关闭: chkconfig iptables off 临时关闭(重启后失效) 开启: service iptables st ...

  10. 学习笔记之DevOps

    DevOps - Wikipedia https://en.wikipedia.org/wiki/DevOps DevOps (a clipped compound of "developm ...