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. VGA原理

    VGA原理 1.VGA时序 2.不同的显示标准,有不同的水平段和垂直段 3.像素时钟和帧频的关系 联系目前调试的1080i 50Hz: 像素时钟为148.5MHz, 水平段周期 = 2640 X (1 ...

  2. ArrayBlcokingQueue,LinkedBlockingQueue与Disruptor三种队列对比与分析

    一.基本介绍 ArrayBlcokingQueue,LinkedBlockingQueue是jdk中内置的阻塞队列,网上对它们的分析已经很多,主要有以下几点: 1.底层实现机制不同,ArrayBlco ...

  3. 代码编辑器之EditPlus

    引用及下载地址:http://www.iplaysoft.com/editplus.html EditPlus是一套功能非常强大的文字编辑器,拥有无限制的Undo/Redo(撤销).英文拼字检查.自动 ...

  4. java读写操作心得

    一.获得控制台用户输入的信息     public String getInputMessage() throws IOException...{         System.out.println ...

  5. awk如何向shell传值

    今天写脚本,遇到awk脚本向shell传参的情况,上网谷歌一下,发现都有些麻烦,通过管道,通过eval,感觉都很复杂.于是想到用read来试一下. 首先构造一个测试文件test.txt,里面的内容是1 ...

  6. Eclipse/MyEclipse向HDFS中如创建文件夹等操作报错permission denied解决办法

    不多说,直接上干货! 问题现象 当执行创建文件的的时候, 即: String Path = "hdfs://host2:9000"; FileSystem fileSystem = ...

  7. Python——pandas读取JSON数据,xml,html数据(python programming)

  8. 在外部怎么调用jquery插件里的function

    文章来源:百度知道 问:(function($){函数(){xxxx}})(jQuery),我怎么调用这个函数呢? (function($){ function render(jq){ 这里是jque ...

  9. 阿里云OSS图片云存储测试上传

    在开发DEMO之前首先要确定 你开发OSS服务并获取了 accessKeyId和accessKeySecret final String key = MD5.Md5(DateFormat.format ...

  10. 06-ICMP: Internet 控制报文协议

    I C M P经常被认为是I P层的一个组成部分.它传递差错报文以及其他需要注意的信息. I C M P报文通常被I P层或更高层协议( T C P或U D P)使用.一些I C M P报文把差错报文 ...