zoukankan      html  css  js  c++  java
  • leetcode python 010

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

    ----蚂蚁不在线
  • 相关阅读:
    centos下安装mycat
    centos下安装MySQL5.7
    centos下yum安装wget失败
    开心消消乐刷金币
    myBatis获取批量插入数据的主键id
    nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
    Mac下安装JDK 6
    VI下删除所有内容
    Mac下lombok无法安装到eclipse mars
    WEB-INF目录下的文件访问权限
  • 原文地址:https://www.cnblogs.com/offline-ant/p/9372078.html
Copyright © 2011-2022 走看看