力扣第十题“正则表达式匹配”(python未解决)

题目:

思路:

这个题目的描述就挺抽象的,然后我以为p中,只会出现一次“*”,结果测试用例里面有多个“*”的,而且C*,代表的是(0~n)C,C*是一个整体,我以为是“*”代表(0~n)个C,麻了,最后想了很久也没有很好的解决方案,有点其它的思路,但是感觉要重写,暂时先缓缓

代码:

class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        mi_position = 0
        position = 0
        temp_list = []
        p_length = len(p)
        s_length = len(s)
        length = s_length-p_length
        flag_mi = True
        temp_list2 = ''
        while p[position] != "*":  #寻找p中*的位置
            position += 1
            if position==p_length:
                 flag_mi =False   #如果p中不存在“*”,flag为False
                 break 
        if flag_mi :              #如果p中存在“*”
            if length >=0:        #s长度大于p
                for i in range(length+1):
                    temp_list.append(p[position-1])
                temp_str =''.join(''.join(row) for row in temp_list)
                temp_list2 = p.split('*')
                temp_list2.insert(1,temp_str)
                temp_str2 =''.join(''.join(row) for row in temp_list2)
            elif length ==-1:
                temp_list2 = p.strip('*')
                temp_str2 = ''.join(''.join(row) for row in temp_list2)
            else:
                return False
        else:
            temp_str2 =p
        if len(temp_str2)!=s_length:
            return False
        for k in range(s_length):
            if s[k] !=temp_str2[k] and temp_str2[k] !=".":
                return False
        return True

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值