码迷,mamicode.com
首页 > 编程语言 > 详细

leetcode python 010

时间:2018-07-26 16:13:14      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:spl   for   部分   return   表达   bsp   nbsp   long   []   

#实现正则表达式匹配并支持‘.‘和‘*‘。
#‘‘匹配任何单个字符。
#‘*‘匹配前面元素的零个或多个。
#匹配应覆盖整个输入字符串(非部分)。
##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

def ismatch(s,re):
    l=[]
    for k in range(len(re)):
        if re[k]==‘*‘:
            l.append(k)
    re=‘‘.join(re.split(‘*‘))
    for i in range(len(l)):
        l[i]=l[i]-i-1
    print(re)
    print(l)
    flg=0
    for i in range(len(s)):
        ## *退出
        while flg  in l and s[i]!=re[flg] and re[flg]!=‘.‘:            
            flg+=1
        if flg==len(re):
            return False,‘re too short‘
        if flg  not in l:
            if s[i]==re[flg] or re[flg]==‘.‘:
                flg+=1
            else:
                return False,‘no *‘
        if i==len(s)-1:
            if flg==len(re):
                return True,‘perfect‘
            else:
                return False,‘re too long‘
       

print(ismatch("aaaaab", "c*a*."))

leetcode python 010

标签:spl   for   部分   return   表达   bsp   nbsp   long   []   

原文地址:https://www.cnblogs.com/offline-ant/p/9372078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!