[LeetCode][Python]Regular Expression Matching
# -*- coding: utf8 -*-
'''
https://oj.leetcode.com/problems/regular-expression-matching/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character.
'*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be:
bool isMatch(const char *s, const char *p) 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 ===Comments by Dabay===
自我感觉很解得很垃圾啊。如果不是把p做了压缩还过不了online judge。其实就是一个DFA。 当s和p都是空的时候,匹配成功。
如果s为空,p不为空,检查p的偶数为是不是都是*。 如果s和p都不为空,头一个字母
如果不匹配,
不带*,False
带*,递归p[2:]
如果匹配,
不带*,递归s[1:],p[1:]
带*,考虑匹配0到最远端的情况,分别递归s[i:],p[2:]
'''
class Solution:
# @return a boolean
def isMatch(self, s, p):
def compress(p):
i = 0
while i < len(p)-3:
if p[i+1] != '*':
i = i + 1
continue
if p[i+3] == '*':
if p[i] == "." or p[i+2] == ".":
p = p[:i] + ".*" + p[i+4:]
continue
elif p[i] == p[i+2]:
p = p[:i] + p[i+2:]
continue
i = i + 2
return p def isMatch2(s, p):
if len(s) == 0:
if len(p) == 0:
return True
if len(p) % 2 == 0:
i = 1
while i < len(p):
if p[i] != "*":
return False
i = i + 2
else:
return True
else:
return False
if len(s) > 0 and len(p) == 0:
return False match_char = p[0]
multi = False
if len(p) > 1:
if p[1] == "*":
multi = True if match_char != s[0] and match_char != ".":
if multi:
return isMatch2(s, p[2:])
else:
return False if multi is False:
return isMatch2(s[1:], p[1:])
else:
result = False
i = 0
result = isMatch2(s, p[2:])
while i < len(s):
if result == True:
return result
if (s[i] == match_char or match_char == "."):
result = isMatch2(s[i+1:], p[2:])
else:
break
i = i + 1
return result return isMatch2(s, compress(p)) def main():
s = Solution()
print s.isMatch("aa", "a*") if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[LeetCode][Python]Regular Expression Matching的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- 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
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- 【leetcode】Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
随机推荐
- highcharts:根据Y的数值范围,动态改变图形的填充颜色
图形实例: 源代码如下: <!DOCTYPE html><html><head><meta charset="utf-8">&l ...
- Recall(召回率);Precision(准确率);F1-Meature(综合评价指标);true positives;false positives;false negatives.
Recall(召回率);Precision(准确率);F1-Meature(综合评价指标);在信息检索(如搜索引擎).自然语言处理和检测分类中经常会使用这些参数. Precision:被检测出来的信息 ...
- Python命令行参数sys.argv[]
学习C语言的时候就没弄明白命令行参数的用法,在学习Pyton 的时候又遇到了命令行参数,在这里稍微学习了一下,稍微明白了一些在这里做个记录方便后面回顾复习. Sys.argv[]是用来获取命令行参数的 ...
- liunx下NetworkManager导致网卡不能启动
前几天在客户现场,配置一台系统为redhat 6.0的服务器,这台服务器是IBM x3755,系统是预装的.在把服务器的IP地址配置完成后,使用命令不能启动网卡.提示:弹出界面 eht0:错误:激活链 ...
- Oracle EBS-SQL (WIP-12):总装车间任务查询.sql
select WT.WIP_ENTITY_NAME 任务名 ...
- Android开发:自定义GridView/ListView数据源
http://mobile.51cto.com/android-259861.htm 在开发中,我们常常会遇到比较复杂的GridView/ListView的布局,重新实现BaseAdapter不但能帮 ...
- 异步操作AsycnTask类
1. 首先执行onPreExecute方法,进行UI的初步设置 2. 其次执行doInBackground方法,此时将不在UI中线程中进行了 3. 然后如果要进行中的数据的话可以通过publis ...
- 多项式ADT的数组实现
/*删除表的正确方法*/ /*assume header*/ void DeleteList(List L) { Position p,Tmp; p=L->Next; while(p != NU ...
- float
.clearfix:after { visibility: hidden; display: block; font-size: 0; content: " "; clear: b ...
- Matrix(类似kruskal)
Matrix Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submis ...