LeetCode-10:Regular Expression Matching

Windows下玩转Spark:零基础本地集群搭建与实战演示 本文详细介绍了在Windows系统下零基础搭建Spark本地集群的完整流程,包括环境准备、集群启动、实战案例演示及性能优化技巧。通过Standalone模式快速部署,结合WordCount等经典案例帮助初学者快速掌握Spark数据处理,特别适合Windows环境下的大数据开发入门学习。 阅读详情

Regular Expression Matching. 正则表达式匹配.

一、题目

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 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 = “cab”

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 = “misisp*.”

Output: false

题目翻译:

给定一个字符串 (s) 和一个字符模式 §。实现支持 ‘.’ 和 ‘*’ 的正则表达式匹配。

‘.’ 匹配任意单个字符

‘*’ 匹配零个或多个前面的那一个元素


二、解题方案

思路:

回溯法:(设s(i)表示串s从位置i开始的后缀。p(j)同理)

1.当s为空串时,此时p为空串,匹配成功。

2.当p为空时,此时s不为空,匹配失败。

3.如果s和p的第一个字符匹配时,即s[0] == p[0] 或 p[0] == ‘.’时,要对除第一个字符外的s(1)和p(1)递归匹配。

4.当p[1] == ‘*’

5.其它情况,匹配失败。

代码实现:

class Solution {
public:
    bool isMatch(string s, string p) {
      if(s.empty()) return p.empty() || p.size() >= 2 && p[1] == '*' && dfs(s,p.substr(2));
        if(p.empty()) return false;
        if(s[0] == p[0] || p[0] == '.') {
            bool r = dfs(s.substr(1),p.substr(1));
            if(r) return true;
        }
        if(!(p.size() >= 2 && p[1] == '*')) return false;
        bool r = dfs(s,p.substr(2));
        if(r) return true;
        for(int c=0;c<s.size();++c){
            if(s[c] == p[0] || p[0] == '.') {
                bool r = dfs(s.substr(c+1),p.substr(2));
                if(r) return true;
            }else break;
        }

        return false;
    }
};
mmdetection 实战:基于Mask R-CNN与自定义COCO数据集的目标检测全流程解析 本文详细解析了如何使用mmdetection框架和Mask R-CNN模型,基于自定义COCO数据集完成目标检测任务的全流程。内容涵盖环境搭建、数据格式转换、配置文件定制化、模型训练与评估等核心步骤,并提供了针对工业缺陷检测场景的实用调优思路与避坑指南,帮助开发者高效构建高性能检测模型。 阅读详情

相关推荐

销售预测的方法与模型(三)丨安全库存与再订货(补货)

较高的 Z 值对应较高的服务水平(例如,服务水平 99% 对应 Z 值 2.33),意味着企业希望更低的缺货风险,因此需要更高的安全库存。在再订货点的管理中,考虑到供应商交货的不确定性,通常需要结合安全库存,以防止在交货期间库存用尽。要对L开根号,根号L表示交货周期越长,企业需要的安全库存越多,因为在较长的供应期内,需求波动的累积效应更明显。举例:供应商的交期服从标准差为2件/天的正态分布,顾客需求为50件/天的正态分布,若要客户服务水平达到 90%,安全库存量应该是多少?下面介绍几种核心的模型与公式。

weixin_45675709的博客 1547

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

西施豆腐渣 2807

WOL唤醒配置(以太网、PHY、MAC)

MAC 就是以太网控制器,属于OSI的数字链路层。phy 属于OSI的物理层(Physical layer),所以叫phy。CPU Mac与phy通信通过MII接口,数据的传输通过MDIO数据线。通过配置phy的相关寄存器实现不同的功能。

qq_58264156的博客 8656

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

Camera mipi通信协议

MIPI(移动行业处理器接口),是的缩写。MIPI移动行业处理器接口)是MIPI联盟发起的为移动应用处理器制定的开放标准。分别定义了一系列的手机内部接口标准,比如摄像头接口CSI、显示接口DSI,其中是由MIPI联盟下Camera工作组指定的接口标准。CSI-2是MIPI CSI第二版,主要由应用层、协议层、物理层组成,最大支持4通道数据传输、单线传输速度高达1Gb/s。

arvin_xiong的博客 3736

DDR Study - LPDDR5 Power-Up and Initialization Sequence

本文深入解析LPDDR5内存的上电初始化流程,涵盖电源斜坡、复位信号释放和初始化训练等关键阶段。通过实际案例分享,详细探讨了电源轨时序、RESET信号处理、ZQ校准和CA总线训练等核心技术要点,帮助硬件工程师规避常见设计陷阱,确保DDR内存稳定运行。

weixin_42523792的博客 306

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

利用eNSP实现校园网多出口策略路由的仿真与优化

本文详细介绍了如何利用华为eNSP仿真工具实现校园网多出口策略路由的配置与优化。通过eNSP模拟真实网络环境,文章展示了从拓扑设计、基础配置到策略路由实现的全过程,帮助网络工程师解决校园网多出口路由的复杂性问题,提升网络访问效率与稳定性。

weixin_29285605的博客 442

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

windows中通过docker部署LocateAnything-3B模型

windows中通过docker部署LocateAnything-3B模型

yilvyangguang520的博客 492

【RTD】二分法查找和分段线性插值算法在RTD中应用

二分法查找和分段线性插值算法在RTD中的典型应用。

只要思想不滑坡,想法总比问题多。 2389

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

SAP S4 HANA:按单生产(MTO)场景中的COGS分割

本文介绍了SAP S/4HANA中提供的各种选项,以在销售订单/商品是按订单生产(以下称为MTO)生产流程中的成本对象时,在基于帐户的COPA环境中报告COGS分割信息。

adam915的博客 1197

LeetCode:10. Regular Expression Matching

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

Wisimer 211

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

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

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

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

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

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

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

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: 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 : 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
上一篇: LeetCode-9:Palindrome Number
下一篇: LeetCode-11:Container With Most Water
7-sevens
博客等级 码龄11年 259粉丝 68原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值