手动实现.*正则表达式匹配函数
发布时间:2020-12-14 00:35:49 所属栏目:百科 来源:网络整理
导读:手动实现.*正则表达式匹配函数 regular expression matching '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Some examples: isMatch("aa","a")
手动实现.*正则表达式匹配函数regular expression matching
Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa","a*") → true isMatch("aa",".*") → true isMatch("ab",".*") → true isMatch("aab","c*a*b") → true isMatch('bbbba','.*a*a') → true isMatch('a','.*..a*') → False isMatch('a','ab*') → true isMatch('ab','.*c') → False 思路
代码class Solution(object): def matchChar(self,sc,pc): return sc == pc or pc == '.' def isEndOfStar(self,p): while p != '': if len(p) == 1 or len(p) > 1 and p[1] != '*': return False p = p[2:] return True def isMatch(self,s,p): if p == '': return s == '' if s == '': return self.isEndOfStar(p) if (len(p) > 1 and p[1] != '*') or len(p) == 1: # without * if not self.matchChar(s[0],p[0]): return False else: return self.isMatch(s[1:],p[1:]) else: # with * # try see x* is empty if self.isMatch(s[0:],p[2:]): return True # x* 可以 代表 x 一到多次 while self.matchChar(s[0],p[0]): s = s[1:] if self.isMatch(s,p[2:]): return True if s == '': return self.isEndOfStar(p) return False 本题以及其它leetcode题目代码github地址: github地址 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Flash AIR)AIR操作zip解压文件到指定目录下
- 从服务器端返回目标文档xml
- Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型
- ruby-on-rails-3 – Rails / Rack:“ArgumentError:inval
- ruby-on-rails – ruby??中有哪些可用的消息解决方案用于进
- C# const:常量定义
- PostgreSQL 9.1主键自动增量
- 正则表达式 – 为什么记事本中的[[:alpha:]]也符合中文字
- 动态添加综合布局---动态添加控件及将某XML动态加入到Activ
- 是否有Oracle的官方文档说我们不应该在新项目中使用java.ut