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. 开源截图工具cutycapt的安装及使用

    之前在安装过程中碰到很多问题,也找了不少资料.现总结了下,给有需要的朋友.centos下安装cutycapt比较麻烦,需要先安装qt47,再下载cutycapt源码编译;而在ubuntu下安装cuty ...

  2. 【java】浅谈swtich

    在java中switch后的表达式的类型只能为以下几种:byte.short.char.int(在Java1.6中是这样),java1.7后支持了对string的判断 switch 的括号一定是表达式 ...

  3. ASP.NET Web APIs 基于令牌TOKEN验证的实现(保存到DB的Token)

    http://www.cnblogs.com/niuww/p/5639637.html 保存到DB的Token 基于.Net Framework 4.0 Web API开发(4):ASP.NET We ...

  4. 固态硬盘SSD,机械硬盘HDD,4K速度对比。

    HDD - SSD -

  5. json server的简单使用(附:使用nodejs快速搭建本地服务器)

    作为前端开发人员,经常需要模拟后台数据,我们称之为mock.通常的方式为自己搭建一个服务器,返回我们想要的数据.json server 作为工具,因为它足够简单,写少量数据,即可使用. 安装 首先需要 ...

  6. Jenkins的详细安装

    操作环境:Windows 一.环境准备 1 安装JDK 本文采用jdk-8u111-windows-x64.exe: 2 配置tomcat 本文采用tomcat8,无需安装,配置JAVA_HOME及J ...

  7. [蓝桥杯]ALGO-15.算法训练_旅行家的预算

    问题描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D1.汽车油箱的容量C(以升为单位).每升汽油能行驶的距离D2.出发点每升汽油价格P和沿 ...

  8. Lucene 特殊字符的问题

    SolrQuerySyntax http://wiki.apache.org/solr/SolrQuerySyntax solr的处理方式: https://svn.apache.org/repos/ ...

  9. print 输出到文件

    content = """We have seen thee, queen of cheese, Lying quietly at your ease, Gently f ...

  10. [Chrome]点击页面元素后全屏

    function isFullScreen() { return (document.fullScreenElement && document.fullScreenElement ! ...