LeetCode 失败的尝试 10. regular expression matching & 正则
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 & 正则的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- 刷题10. Regular Expression Matching
一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- leetcode problem 10 Regular Expression Matching(动态规划)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【一天一道LeetCode】#10. Regular Expression Matching
一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...
- 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
随机推荐
- piwik获取访客头像,自定义显示访问者头像(URL)和描述(标题和替代)
访客头像 自定义显示访问者头像(URL)和描述(标题和替代) 链接地址:https://plugins.matomo.org/VisitorAvatar#description
- 安装使用git
阿斯蒂芬 http://www.cnblogs.com/alex3714/articles/5930846.html
- 测试教程网.unittest教程.3. 实例: 测试弱密码
From: http://www.testclass.net/pyunit/test_example_1/ 背景 考虑这样一个测试弱密码的实例,这个我们在pytest相关教程中也有过描述. 我们需要判 ...
- 引用 自动化测试基础篇--Selenium Python环境搭建
原文链接:https://www.cnblogs.com/sanzangTst/p/7452922.html 鸣谢参藏法师. 学习selenium python需要的工具: 1.浏览器 2.Pytho ...
- [C#][Quartz]添加监听器
namespace Quartz.Listener { public class SchedulerListener : SchedulerListenerSupport { private stat ...
- 符合seo的html标签优化
原文地址:http://www.rainleaves.com/html/1032.html seo(Search Engine Optimiztion)搜索引擎优化的英文缩写.通过总结搜索引擎的 ...
- 事件之onTouch方法的执行过程 及和 onClick执行发生冲突的解决办法
转载:http://blog.csdn.net/jiangwei0910410003/article/details/17504315#quote 博主推荐: 风萧兮兮易水寒,“天真”一去兮不复还.如 ...
- [UE4]Overlay
Overlay容器的子控件叠加,没有z-order属性设置,只能在编辑器中调整前后层级,也不能设置位置.可以理解是Canvas Panel的简化控件
- [UE4]在UI中获取玩家角色实例
- CountDownLatch的简单讲解
正如每个Java文档所描述的那样,CountDownLatch是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行.在Java并发中,countdownlatch的概念是一 ...