LeetCode #10 - Regular Expression Matching

【推荐100个unity插件之24】实现2d交互式草树叶和随风飘动的效果——pixelgraphics插件 【推荐100个unity插件之23】实现2d交互式草树叶和随风飘动的效果——pixelgraphics插件 阅读详情

题目描述:

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

递归解法:由于x*可以表示‘’、x、xx、xxx.....,所以①当p[1]!='*'时,如果s[0]!=p[0]则不可能匹配,而当s[0]=p[0]时,还要满足s.substr(1)和p.substr(1)匹配才行。②当p[1]='*'时,则p[0]p[1]连起来可以表示p[0]重复0次、1次...n次,所以如果是p[0]重复0次的情况,则还需要s与p.substr(2)匹配才行;如果是p[0]重复多次,那么p[0]必须等于s[0],否则肯定不能匹配,如果s[0]=p[0],那么将s[0]除去后,此时判断s.substr(1)与p是否匹配即可。因此在p的长度大于等于2的情况下是可以进行递归的,而且由于递归过程中需要s[0]的值,所以s不能为空。

接下来单独处理p长度为1或0情况:①如果p和s都为空,则匹配;②如果p为空而s不为空,则不匹配;③如果s不为空,而p的长度为1,那么s的长度必须为1,且s[0]=p[0],否则不能匹配。

以及s为空的情况:①如果s为空而p不为空,只有在p形如a*b*c*d*....n*的情况下才能匹配,所以p的长度必须为偶数且偶数位元素必须为‘*’,这种情况可以用递归解决。

另外需要注意的是,p[i]='.'时可以等于任何字符。

class Solution {
public:
    bool isMatch(string s, string p) {
        if(s.size()==0&&p.size()==0) return true;
        else if(s.size()!=0&&p.size()==0) return false;
        else if(s.size()==0&&(p.size()%2==0)&&p[1]=='*') return isMatch(s,p.substr(2));
        else if(s.size()==0) return false;
        else if(s.size()!=0&&p.size()!=0)
        {
            if(p.size()==1)
            {
                if(s.size()==1&&(s[0]==p[0]||p[0]=='.')) return true;
                else return false;
            } 
            else if(p[1]!='*')
            {
                if(s[0]==p[0]||p[0]=='.')
                    return isMatch(s.substr(1),p.substr(1));
                else return false;
            }
            else if(p[1]=='*')
            {
                if(isMatch(s,p.substr(2))) return true;
                else if(s[0]==p[0]||p[0]=='.')
                    return isMatch(s.substr(1),p);
                else if(s[0]!=p[0]&&p[0]!='.') return false;
            }
        }
    }
};

递归的方法较为耗时,还可以采用动态规划的方法:①先将两个字符串在首部加上空格字符,便于进行递推。②定义二维数组x[m][n],m、n为s、p的长度,x[i][j]表示s[0]-s[i]和p[0]-p[j]是否匹配,先将二维数组的首行和首列初始化。③进行动态规划,两层循环对数组依次赋值:1、如果p[j]不为‘*’,那么只有p[j]=s[i]且s[0]-s[i-1]和p[0]-p[j-1]匹配,s[0]-s[i]和p[0]-p[j]才能匹配,即x[i][j]为真,否则x[i][j]为假。2、如果p[j]为‘*’,那么如果x[i][j-2]为真则x[i][j]为真,即此时p[j-1]重复0次;如果x[i-1][j]为真,说明s[0]-s[i-1]和p[0]-p[j]匹配,那么只有p[j-1]=s[i]的情况下,s[0]-s[i]和p[0]-p[j]才能匹配;其他的情况x[i][j]都为假。递推结束后返回x[i-1][j-1]就是两个字符串匹配的结果。

class Solution {
public:
    bool isMatch(string s, string p) {
        s=" "+s;
        p=" "+p;
        int m=s.size();
        int n=p.size();
        bool x[m][n];
        x[0][0]=true;
        for(int i=1;i<m;i++)
        {
            x[i][0]=false;
        }
        for(int j=1;j<n;j++)
        {
            if(p[j]=='*'&&x[0][j-2]==true)
            {
                x[0][j]=true;
            }
            else x[0][j]=false;
        }
        for(int i=1;i<m;i++)
        {
            for(int j=1;j<n;j++)
            {
                if(p[j]!='*')
                {
                    if((s[i]==p[j]||p[j]=='.'))
                         x[i][j]=x[i-1][j-1];
                    else x[i][j]=false;
                }
                else if(p[j]=='*')
                {
                    if(x[i][j-2]==true) x[i][j]=true;
                    else if(x[i-1][j]==true&&(s[i]==p[j-1]||p[j-1]=='.')) x[i][j]=true;
                    else x[i][j]=false;
                }           
            }
        }
        return x[m-1][n-1];
    }
};


适航规章、标准,书籍资料下载汇总(更新中) 以下是一些收集到的适航相关的资料,分享给大家。(如果没有你想要的资料,请留言,等找到会及时更新! 阅读详情

相关推荐

基于DPLL算法解决3-SAT问题(C++实现)

近日看到一个3-SAT问题(Boolean SATisfiability Problem, 布尔逻辑可满足性问题),试着写了个算法求解。

qq_63241496的博客 2214

【一天一道LeetCode】#10. Regular Expression Matching

一天一道LeetCode(一)题目 Implement regular expression matching with support for ‘.’ and ‘*’. ‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding element. The

ZeeCoder 1011

STM32CUBEMX 使用教程5 — DMA配置 & 串口结合DMA实现数据搬运

DMA(Direct Memory Access),直接存储器访问(或数据搬运),是STM32中很实用的一个外设。DMA 传输数据方式不需要 CPU 参与控制传输,也不用中断处理的方式进行数据搬运处理,纯粹通过硬件为 RAM 与 I/O 设备之间开辟一条直接传送数据的通路,使 CPU 的效率大大提高。

weixin_43866583的博客 1499

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

王旻宇 395

cutlass2.x源码解读之PitchLinearStripminedThreadMap

TensorCoord:使用layout::PitchLinearCoord作为张量坐标。Shape:使用模板参数Shape_作为tile的形状,kThreads:线程总数,定义为模板参数Threads。kElementsPerAccess:每次访问的元素数,定义为模板参数ElementsPerAccess。ThreadAccessShape:线程访问的形状,使用layout::PitchLinearShape定义。内部实现细节:ShapeVec。

jiangjkd的专栏 786

实战指南:利用 LangGraph 的状态管理实现多轮对话的上下文隔离与持久化

本文详细介绍了如何利用LangGraph的状态管理机制,高效实现多轮对话中的上下文隔离与持久化。通过实战代码演示,从基础的消息历史管理到自定义状态扩展,再到使用SQLite进行生产级持久化,系统讲解了如何借助LangGraph的State和Checkpointer自动化处理对话记忆、用户隔离及数据存储,显著提升开发效率。

weixin_28396629的博客 488

LeetCode #10 Regular Expression Matching

Description 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 in

菜鸟程序员的成长历程 513

Isaac Sim/Lab & ROS2 入门教程 —— 宇树人形机器人 H1 强化学习策略部署

本文介绍了如何通过ROS2和IsaacSim运行H1人形机器人的强化学习运动策略。主要内容包括:1)设置机器人关节配置和初始位置;2)添加IMU传感器;3)配置ROS2节点以发布观测数据和接收动作指令;4)创建模拟环境并发布ROS时钟;5)最终运行ROS2策略实现对机器人的键盘控制。文中详细说明了关节参数设置、传感器配置、ROS2节点创建等关键步骤,并提供了注意事项和验证方法。完成配置后,用户可通过键盘指令控制机器人在平坦地形上的运动。

2506_90492529的博客 2169

java regular expression matching_LeetCode #10 Regular Expression Matching 正则表达式匹配

Description: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 e...

weixin_39808953的博客 211

后摄模块辐射超标整改

某行车记录仪在EMI测试中出现辐射超标问题,通过模块剥离法定位到后摄模块的Ethernet数据线为噪声源。深入检查发现TYPE-C延长线屏蔽层未有效接地,形成辐射泄漏通道。整改措施包括加强屏蔽层360°接地、使用导电布改善连接处屏蔽等,最终使辐射降至EN55032 Class B限值以内。案例揭示了高速信号延长线中屏蔽连续性的重要性,建议设计阶段就重视连接器屏蔽设计和共模干扰抑制。

硬核科技 255

YOLO26涨点改进 | 独家创新,注意力改进篇 | AAAI 2025 | YOLO26引入 DRM 防御优化模块,进行特征优化/特征增强,助力目标检测、图像分类、图像分割有效涨点

本文提出了一种基于DRM防御优化模块改进的YOLO26网络模型,显著提升了目标检测的鲁棒性和特征提取能力。DRM模块通过优化特征提取过程、增强抗噪声能力和补充细节信息,有效提高了模型在复杂场景下的检测精度和定位准确度。该模块可集成于UNet等主干网络,采用Transformer结构实现特征精炼,在对抗攻击环境下仍能保持稳定的融合效果。实验表明,改进后的模型在红外与可见光图像融合任务中表现出色,同时提升了下游任务性能。文章详细介绍了DRM模块的结构、作用机制及实现代码,并提供了两种YOLO26改进方案.

Ai缝合怪、助力小伙伴高效发论文 141

[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 en...

weixin_30274627的博客 74

AI 实战篇 |基于 AI开放平台实现 人脸识别对比 功能,超详细教程【附带源码】

本篇文章来学习一下怎样使用 Unity实现简单地人脸识别 功能。 关于人脸识别,我们可以选择很多平台的SDK接入Unity来实现人脸识别的功能。

努力前行,总会成为自己心中的那道光 5万+

[LeetCode]#10 Regular Expression Matching javascript字符串包含

https://leetcode.com/problems/regular-expression-matching/ * isMatch.js /** * @param {string} s: text * @param {string} p: pattern * @return {boolean} */ var isMatch = function(s, p) { if ...

fareast_mzh的博客 443

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

[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 inp...

weixin_30507481的博客 71

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

LeetCode #10: Regular Expression Matching

Problem Statement(Source) Implement regular expression matching with support for '.' and '*'.'.' Matches any single character. '*' Matches zero or more of the preceding element.The matching should cove

浮生琴弦 299

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

(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 input st

fssssssss的专栏 772

【LeetCode】#10正则表达式匹配(Regular Expression Matching)

【LeetCode】#10正则表达式匹配(Regular Expression Matching) 题目描述 给定一个字符串 (s) 和一个字符模式 §。实现支持 ‘.’ 和 ‘’ 的正则表达式匹配。 ‘.’ 匹配任意单个字符。 '’ 匹配零个或多个前面的元素。 匹配应该覆盖整个字符串 (s) ,而不是部分字符串。 说明: s 可能为空,且只包含从 a-z 的小写字母。 p 可能为空,且只包含从 ...

weixin_43858604的博客 221

LeetCode[动态规划] - #10 Regular Expression Matching

原题链接:#10 Regular Expression Matching   要求: 实现正则表达式匹配,支持'.'和'*'。   '.'匹配任意单字符。 '*'匹配任何0个或多个之前元素。   匹配应当覆盖整个输入字符串,而不仅仅是子串。   函数原型: boolean isMatch(String /* string to check */ s, String /* p...

Stay hungry, stay foolish. 243

Leetcode #10 Regular Expression Matching 正则表达式匹配 解题小节

1 原题原题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

MebiuW的专栏 1049

[ LeetCode ] #10. Regular Expression Matching(正则表达式匹配 C++ & Python)

题目:10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matche...

xn's blog 219

【LeetCode】10.Regular Expression Matching(Python)

Problem LeetCode 10: 正则表达式匹配 难度:hard 给定输入字符串和模式(P),实现与“.”和“”支持匹配的正则表达式。 “.”匹配任何单个字符。 “”与前面的单个元素零个或多个匹配。 匹配应该覆盖整个输入字符串(而不是部分)。 注: s可以为空,并且只包含小写字母a-z。 p可以为空,只包含小写字母a-z和类似的字符。或者*。 例1: 输入:S=“AA”, P=“A” 输出...

E.W的博客 1450

LeetCode Regular Expression Matching

简易版正则表达式匹配,只有两种通配符,”.”表示任意一个字符,”c*”表示字符c可以有零个或多个。

DRFish 2032

10. Regular Expression Matching Leetcode Python

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

hyperbolechi的专栏 1467

leetcode 10 Regular Expression Matching & 44 Wildcard Matching

leetcode中的两个 正则表达式匹配

ghoshorn的专栏 733

Leecode #10 Regular Expression Matching

一、 问题描述 Leecode第十题,题目为: Given an input string (s) and a pattern §, implement regular expression matching with support for ‘.’ and ‘*’. ‘.’ Matches any single character. ‘*’ Matches zero or more of the...

serena_t的博客 146
上一篇: LeetCode #4 - Median of Two Sorted Arrays
下一篇: LeetCode #19 - Remove Nth Node From End of List
LawFile
博客等级 码龄10年 5粉丝 627原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值