LeetCode:10. Regular Expression Matching

【A2DP】规范精讲[1]: 深入解析A2DP协议的核心架构 在蓝牙技术发展的早期阶段,音频传输主要通过SCO(同步面向连接)链路实现,这种设计主要针对语音通信,音质有限且功能单一。随着人们对无线高质量音频需求的增长,​​Advanced Audio Distribution Profile (A2DP)​​ 应运而生,成为蓝牙音频传输的技术基石。 阅读详情

LeetCode:10. Regular Expression Matching

Given an input string (s) and a pattern (p), 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).

Note:

s could be empty and contains only lowercase letters a-z.
p could be empty and contains only lowercase letters a-z, and characters like . or *.

Example 1:

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".

Example 3:

Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".

Example 4:

Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".

Example 5:

Input:
s = "mississippi"
p = "mis*is*p*."
Output: false

正则表达式匹配,这里只用了两个符号:* 和 .

思路一:递归

首先明确的是 * 只会出现在一个字符或者 . 之后。

  1. 如果text和pattern第一个字符匹配(相等或者patter第一个字符为.)且pattern第二个字符不是 * ,则递归判断 text 从第二个字符开始向后的子串以及pattern第二个字符向后的子串。
  2. 如果pattern第二个字符是 * ,有两种情况:
    (1) * 匹配了0个字符,递归判断text以及patter从第三位开始向后的子pattern。也就是 self.isMatch(text, pattern[2:])。这种情况可以保证第一个字符不相等的时候跳过pattern的前两位。
    (2) * 匹配了一个或多个字符,这种情况需要第一个字符相等,也就是下面的 first_match 为True,然后递归判断text第二个字符开始的子串以及整个pattern。这种情况保证了pattern第一个字符和 * 必须至少一个字符,然后向后匹配多个相同的字符。

Python 代码实现

class Solution(object):
    def isMatch(self, text, pattern):
        if not pattern:
            # 如果pattern为空,则返回text是否为空
            return not text

        # 判断第一个字符是否匹配
        first_match = bool(text) and pattern[0] in {text[0], '.'}

        if len(pattern) >= 2 and pattern[1] == '*':
            # 注意这里:self.isMatch(text, pattern[2:])表示X*匹配0个字符,如果这种情况可以匹配成功直接返回True。否则就让X*匹配一个或多个字符,这种情况首先就要保证first_match为True
            return (self.isMatch(text, pattern[2:]) or (first_match and self.isMatch(text[1:], pattern)))
        else:
            return first_match and self.isMatch(text[1:], pattern[1:])
还有一种从后向前的暴力求解法:

* 不会单独出现,它一定是和前面一个字母或".“配成一对。看成一对后"X*”,它的性质就是:要不匹配0个,要不匹配连续的“X”.所以尝试暴力解法的时候一个trick是从后往前匹配.

是这样来分情况看的:

  1. 如果s[i] = p[j] 或者 p[j]= . ,则往前匹配一位。
  2. 如果p[j] = * , 检查一下,如果这个时候p[j-1] = . 或者p[j-1] = s[i] ,那么就往前匹配,如果这样能匹配过,就return True(注意如果这样不能最终匹配成功的话我们不能直接返回False,因为还可以直接忽略' X* '进行一下匹配试试是否可行), 否则我们忽略 ’ X* '(也就是匹配0个字符),这里注意里面的递推关系。
  3. 再处理一下边界状况:
    • s已经匹配完了, 如果此时p还有,那么如果剩下的是 X* 这种可以过,所以检查
    • p匹配完毕,如果s还有那么报错

Python 代码实现

class Solution(object):
    def isMatch(self, s, p):
        def helper(s, i, p, j):
            if j == -1:
                #模式串遍历完成,看原始字符串有没有遍历完成
                return i == -1
            if i == -1:
                if p[j] != '*':
                    #原始字符串有没有遍历完成,模式串如果不是*肯定不能匹配完成
                    return False 
                else:
                    #原始字符串有没有遍历完成,模式串还有*,继续匹配
                    return helper(s, i, p, j-2)
            if p[j] == '*':
                if p[j-1] == '.' or p[j-1] == s[i]:
                    if helper(s, i-1, p, j):
                        # 试探性向前匹配以为,所以这里的j不变,如果成功则返回True
                        return True
                    else:
                        # 如果成功则跳过这两个字符,j-2,继续匹配
                        return helper(s, i, p, j-2)
            if p[j] == '.' or p[j] == s[i]:
                # 单个字符匹配
                return helper(s, i-1, p, j-1)
            return False

        return helper(s, len(s)-1, p, len(p)-1)
思路二:动态规划

设dp(i, j)表示: text[i:] 和 pattern[j:] 匹配,将每个(i,j)保存起来。最后返回dp(0,0)就是要求的text和pattern是否匹配

Python 代码实现

# dp
class Solution(object):
    def isMatch(self, text, pattern):
        memo = {}
        def dp(i, j):
            if (i, j) not in memo:
                if j == len(pattern):
                    ans = i == len(text)
                else:
                    first_match = i < len(text) and pattern[j] in {text[i], '.'}
                    if j+1 < len(pattern) and pattern[j+1] == '*':
                        ans = dp(i, j+2) or first_match and dp(i+1, j)
                    else:
                        ans = first_match and dp(i+1, j+1)

                memo[i, j] = ans
            return memo[i, j]

        return dp(0, 0)

THE END.

大中型超市收银系统(可直接运行) 光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开 立即下载

相关推荐

警惕AI依赖:如何避免数字水蛭侵蚀你的认知能力

在人工智能技术日益普及的今天,人机协作已成为现代工作流的核心范式。其原理在于通过算法模型辅助人类完成信息处理、内容生成等任务,从而提升效率。然而,过度依赖AI可能导致认知卸载,引发思考能力退化等潜在风险。从技术价值看,AI的核心在于增强而非替代人类智能,关键在于建立健康的协作模式。在应用场景上,无论是编程开发、内容创作还是决策分析,用户都需警惕工具依赖对深度工作、创造性思维和决策责任的侵蚀。本文聚焦于识别AI依赖的早期信号,并提供构建分阶段使用流程、将AI作为质疑者等具体策略,旨在帮助读者守护核心认知能力,

weixin_30240349的博客 638

leetcode 10: Regular Expression Matching

Regular Expression Matching Total Accepted: 39732 Total Submissions: 192279 Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Mat

西施豆腐渣 2806

光学遥感显著目标检测初探笔记总结

一张图片里最吸引注意力的部分就是显著性物体,其实是模拟人的一个注意力机制。目标是希望通过计算机的方法让我们自动定位和模拟人的这种感知能力,从而去定位场景中的一个让人感兴趣的目标。

醋酸洋红的博客 2090

leetcode题解:第10题Regular Expression Matching

https://leetcode-cn.com/problems/regular-expression-matching/ 分析 这道题的难点在于存在a*这样的组合,如何处理与这种组合匹配的字符数是很棘手的问题,例如: s = "aaa", p = "aa*" s = "baaa", p = "ba*" 第一个例子中a*匹配2个字符,第二个例子中a*匹配3个字符。可以看出,匹配的字符数不仅与s有关,还与p中组合前面的字符有关(后面也有可能)。如果是.*这样的组合,会更加麻烦。 我一开始的想法是用双指针分别

chenf1999的博客 313

从光斑图到锤形优化:一次搞定Zemax双胶合透镜的像散与场曲问题

本文详细解析了Zemax双胶合透镜设计中像散与场曲问题的诊断与优化方法。从光斑图分析到锤形优化技巧,提供了完整的光学设计实战指南,帮助设计师快速定位问题并有效改善系统性能,特别适合光学设计工程师和Zemax使用者参考。

weixin_27134495的博客 240

HDMI调试避坑指南:从AVI InfoFrame到通用控制包,这些DataIsland Packet细节千万别忽略

本文深入解析HDMI调试中DataIsland数据包的关键细节,包括AVI InfoFrame、通用控制包和音频时钟再生包(ACR)的配置与故障排查。通过实际案例和寄存器配置示例,帮助工程师快速定位显示异常、色彩失真和音频中断等问题,提升HDMI接口调试效率。

weixin_30897233的博客 641

LeetCode 10: 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 inpu

哈哈的个人专栏 2717

VS2022 LNK1168无法打开进行写入 解决办法

C语言编程---VS---错误和警告---链接器工具错误---LNK1168--无法打开进行写入

weixin_67166415的博客 7171

LeetCode10:Regular Expression Matching

Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. ...

卷卷萌的博客 247

从CORDIC到DDS:手把手拆解《Verilog传奇》第9章的实战项目(附可跑通的代码)

本文详细解析了如何从CORDIC算法到DDS系统的完整实现过程,基于《Verilog传奇》第9章的实战项目,提供了可运行的Verilog代码。内容涵盖CORDIC算法的硬件实现、DDS系统架构设计、完整系统集成与验证,以及实际部署中的性能调优技巧,适合数字IC和FPGA开发者深入学习。

weixin_42623010的博客 418

SolidWorks 小车底盘 装配体

通过制作小车底盘了解solidworks装配体的部分知识

及时总结 2239

leetcode 10: Regular Expression Matching 分析及解答

解读: •‘.'代表一个任意字符,与其附近的字符无关 •’*‘代表o个或者多个前面的字符,该字符影响前面字符的“存在”,例如:a*={ε,a,aa,aaa,…},即当"*"表示0个前面的字符时,该字符串为空串ε。 分析:题目要求匹配整个输入字符串,即完全匹配,分如下情况: 1.同时为空则true p为ε时,s为ε则匹配 s为ε时,p为ε则匹配,但此时p分两种情况: 1、p确实为ε 2、p为”a*“类型,此时*代表前面的字符存在0次,则p在匹配意义上位ε 2.不同时为空就需要比较 •p字符串中当前字

若有所思HS 1736

YOLO26改进策略【卷积层】| 引入注意力卷积模块RFAConv,关注感受野空间特征 助力YOLO26精度提升

RFAConv:创新空间注意力和标准卷积运算感受野空间特征是针对卷积核设计的,根据卷积核大小动态生成。以3×3卷积核为例,它由非重叠滑动窗口组成,每个窗口代表一个感受野滑块。

Limiiiing的博客 201

Leetcode10:Regular Expression Matching

题目描述 Given an input string (s) and a pattern (p), implement regular expression matching with support for ‘.’ and ‘*’. '.' Matches any single character. '*' Matches zero or more of the preceding elemen...

leihanwen的博客 240

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

LeetCode 10:《Regular Expression Matching》

参考文章:http://articles.leetcode.com/2011/09/regular-expression-matching.htmlLeetCode 10:《Regular Expression Matching》参考C语言代码实现,Java代码实现方式如下: public boolean isMatch(String s, String p) { //

事了拂衣去,深藏功与名 627

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

LeetCode: 10. Regular Expression Matching

Given an input string and a pattern , implement regular expression matching with support for and where:The matching should cover the entire input string (not partial).Example 1: Input: s = "aa", p = "a" Output: false Explanation: "a" does not match the

小猪爱拱地 308

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

Leetcode Q10: Regular Expression Matching

题目10: (该题目拿到手没什么特别好的思路,从网上看的别人的解法,然后写了下自己的理解,需要常回顾) Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding

qwe_lingkun的专栏 494

Leetcode1-50: 10. Regular Expression Matching

Leetcode1-50: 10. Regular Expression Matching问题描述解题思路递归法动态编程代码实现implement 1 递归法implement 2 动态规划法复杂度分析分析 问题描述 Given an input string (s) and a pattern §, implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*

Y123iwantit的博客 324

LeetCode-10:Regular Expression Matching

Regular Expression Matching. 正则表达式匹配. 一、题目 Given an input string (s) and a pattern §, implement regular expression matching with support for ‘.’ and ‘*’ ‘.’ Matches any single character. ‘*’ Matches ...

7-SEVENS 248

java regular expression matching_LeetCode第[10]题(Java):Regular Expression Matching

题目:匹配正则表达式题目难度:hard题目内容:Implement regular expression matching with support for'.'and'*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover ...

weixin_35687571的博客 166

LeetCode题目: 10. Regular Expression Matching

LeetCode题目: 10. Regular Expression Matching 原题链接:https://leetcode.com/problems/regular-expression-matching/description/ 解题思路:此题输入为两个字符串s和p,题目要求是完全匹配,即存在一种可能,使s==p。‘.’意为随便代表一个字符。‘*’意为可匹配一个或多个前继字符。(PS:

MyCodecOdecoDecodE的博客 630

Leetcode: 10. Regular Expression Matching

Description Implement regular expression matching with support for '.' and '*'. Example '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should c...

anrui0096的博客 190

Leetcode 10: Regular Expression Matching

Leetcode 10: Regular Expression MatchingImplement regular expression matching with support for ‘.’ and ‘*’. ‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding element.

丫丫狸笫的专栏 405

LeetCode : No10 Regular Expression Matching

题目链接: https://leetcode.com/problems/regular-expression-matching/ Implement regular expression matching with support for'.' and '*'. '.' Matches any single character. '*' Matches zero or more of t

CoderPlay 310

leetcode:Regular Expression Matching [10]

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 st

愤怒的菜鸟专栏 301

LeetCode10: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

henryzhihua 827
上一篇: LeetCode:55. Jump Game
下一篇: LeetCode:21. Merge Two Sorted Lists
XP-Code
博客等级 码龄12年 113粉丝 194原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值