Leetcode:正则表达式.
来源:互联网 发布:小明发布永久域名台湾 编辑:程序博客网 时间:2024/06/16 07:40
这题比较难,需要采取递归下降的方式来解析数据,并处理边界问题。
class Solution(object): def isMatch(self, s, p): print(s,p,'...') i,n = 0,len(p) if s=='':return p=='' or (len(p)>=2 and p[1]=='*' and self.isMatch('',p[2:])) if p=='':return False if len(p)==1:return (s[0]==p[0] or p[0]=='.') and self.isMatch(s[1:],p[1:]) if s[-1]!=p[-1] and p[-1]!='.' and p[-1]!='*' :return False ch = p[0] if ch=='.': if p[1]=='*': j,n_s = 0,len(s) while j<n_s+1: res = self.isMatch(s[j:],p[2:]) if res==False:pass else:return True j+=1 return False else:#p[i+1] is normal charcter return self.isMatch(s[1:],p[1:]) else:#normal charcter if p[1]=='*':#match 0 or more j = 0 n_s = len(s)-1 while j<=n_s+1: if (j>0 and s[j-1]!=ch):break; print(j,s,p,ch,s[j-1],n_s) res = self.isMatch(s[j:],p[2:]) if res==False:pass else:return True j+=1 return False else: return ch==s[0] and self.isMatch(s[1:],p[1:])print(Solution().isMatch("ab","a*"))
0 0
- Leetcode:正则表达式.
- leetcode-10-正则表达式
- Leetcode 之正则表达式 RegExp
- LeetCode 10 正则表达式匹配
- LeetCode正则表达式-Regular Expression Matching
- *LeetCode 10 Regular Expression Matching 正则表达式
- LeetCode-Valid Number - 有限状态机 & 正则表达式
- 正则表达式匹配-leetcode Regular Expression Matching
- 【正则表达式】正则表达式
- 判断正则表达式是否匹配 Regular Expression Matching @LeetCode
- LeetCode OJ 之 Regular Expression Matching (正则表达式匹配)
- LeetCode 10 Regular Expression Matching (正则表达式匹配)
- leetcode系列(34)Regular Expression Matching 正则表达式匹配
- Leetcode #10 Regular Expression Matching 正则表达式匹配 解题小节
- LeetCode 10. Regular Expression Matching(正则表达式匹配)
- leetcode(10) - Regular Expression Matching 正则表达式匹配
- leetcode--Python正则表达式解析Valid Phone nums
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
- C#之方法
- 各大网站幻灯片Flash广告的ASP.Net/C#版
- 版本更新 配置 value—String文件内容
- Eclipse插件推荐安装列表
- 汇编语言第一个程序
- Leetcode:正则表达式.
- 欢迎使用CSDN-markdown编辑器
- 学习FPGA过程中遇到的问题:QuartusII
- 最简单的ImageLoader工具类
- backbone(4) 事件 model 事件
- 浅谈我对产品需求与软件需求的理解——来源于我的求职面试经历
- HDU 2084 数塔 DP .
- http://www.cnblogs.com/bastard/archive/2013/01/28/2879884.html
- JSON数据格式和serialize数据格式的异同和使用