LeetCode : Regular Expression Matching [java]

LeetCode(10):正则表达式匹配 Regular Expression Matching(Java) 2019.4.20 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) 今天看了一下阿里的Java代码规范,发现if和else不带括号并不是简洁的一种表现,以后会好好加括号的。 这道题之前有在《剑指Offer》上做过原题(笔记传送门:#数据结构与算法学习笔记#剑指Offer50:正则表达式匹配(Java、C/C++)),没想到LeetCode再刷一次的时候,所用的递归方法居然... 阅读详情

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
思路:分情况讨论: 1. P为空串,S是否为空决定结果;2. P长度为1,S根据P的值(./*/其他)确定;3. P长度大于1,第二位不是*,直接匹配第一位,然后截断字符串递归,第二位是*,判断*前的字符出现0次、1次或多次能否正确匹配.
public class Solution {
    public boolean isMatch(String s, String p) {
		// base case
		if (p.length() == 0) {
			return s.length() == 0;
		}

		// special case
		if (p.length() == 1) {

			// if the length of s is 0, return false
			if (s.length() < 1) {
				return false;
			}

			// if the first does not match, return false
			else if ((p.charAt(0) != s.charAt(0)) && (p.charAt(0) != '.')) {
				return false;
			}

			// otherwise, compare the rest of the string of s and p.
			else {
				return isMatch(s.substring(1), p.substring(1));
			}
		}

		// case 1: when the second char of p is not '*'
		if (p.charAt(1) != '*') {
			if (s.length() < 1) {
				return false;
			}
			if ((p.charAt(0) != s.charAt(0)) && (p.charAt(0) != '.')) {
				return false;
			} else {
				return isMatch(s.substring(1), p.substring(1));
			}
		}

		// case 2: when the second char of p is '*', complex case.
		else {
			// case 2.1: a char & '*' can stand for 0 element
			if (isMatch(s, p.substring(2))) {
				return true;
			}

			// case 2.2: a char & '*' can stand for 1 or more preceding element,
			// so try every sub string
			int i = 0;
			while (i < s.length() && (s.charAt(i) == p.charAt(0) || p.charAt(0) == '.')) {
				if (isMatch(s.substring(i + 1), p.substring(2))) {
					return true;
				}
				i++;
			}
			return false;
		}
    }
}


Cadence Virtuoso IC617:从零构建传输门与反相器的Schematic设计实战 本文详细介绍了使用Cadence Virtuoso IC617进行传输门与反相器Schematic设计的完整流程,包括环境准备、原理图绘制、Symbol创建、仿真验证及实用技巧。通过分步指导和实战经验分享,帮助初学者快速掌握集成电路设计基础,提升EDA工具应用能力。 阅读详情

相关推荐

【论文学习】《Fully-Convolutional Siamese Networks for Object Tracking》2016年SiamFC 全卷积孪生网络用于目标跟踪

在这项工作中,我们脱离了跟踪中采用的传统在线学习方法,并展示了一种专注于在离线阶段学习强嵌入的替代方法。与在分类设置中的使用不同,我们证明,对于跟踪应用,孪生全卷积深度网络能够更有效地使用可用数据。这不仅反映在测试时,通过执行有效的空间搜索,也反映在训练时,其中每个子窗口都有效地代表一个有用的样本,而几乎没有额外的成本。实验表明,深度嵌入为在线跟踪器提供了自然丰富的特征来源,并使简单的测试时策略表现良好。我们相信这种方法是对更复杂的在线跟踪方法的补充,并期望未来的工作能够更彻底地探索这种关系。

SkyLeopard的博客 2427

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 string

ithouse的专栏 597

睿尔曼复合机器人之底盘操作流程

控制键”按下后,可在键盘上通过‘i’(向前),‘k’(向后),‘j’(向左旋转),‘l’(向右旋转)四个按键对底盘进行运动控制。选中禁⾏线功能后,使⽤⿏标在界⾯内点击选择起点,继续在其他地⽅点击,两点之间会连成⼀条线,可以进⾏多次连续点击,形成连续线条。4)扫图完成后,软件会重新启动,返回到监控页面,点击“激光”可看到当前机器激光扫描的实时数据,在页面上表现为红色边界线,点击菜单栏“控制”键使底盘在一定范围内运动,扫描到尽可能多的边界线,可发现与实际地图的黑色边界线不同,需要进行手动校正。

realman_Rop的博客 1482

java regular expression matching_[Leetcode] Regular Expression Matching 正则表达式匹配

Regular Expression MatchingImplement regular expression matching with support for '.' and'*'.'.' Matches any single character. '*' Matches zero or more of the preceding element.The matching should cov...

weixin_33293612的博客 205

在Ubuntu 20.04上,用EtherCAT Igh 1.6.2驱动高创BD3E伺服电机(附完整C代码)

本文详细介绍了在Ubuntu 20.04实时内核环境下,使用EtherCAT Igh 1.6.2主站驱动高创BD3E伺服电机的完整实现方案。从实时系统配置、伺服电机PDO映射到精确控制线程设计,提供了完整的C代码样例和实际项目中的调试技巧,帮助开发者实现工业自动化中的高精度运动控制。

weixin_30772105的博客 117

从圆形到椭圆:CenterNet中高斯热图半径的自适应优化策略

本文深入探讨了CenterNet目标检测模型中高斯热图生成策略的优化,针对传统圆形高斯核在处理长宽比差异大目标时的局限性,提出了自适应椭圆高斯核的解决方案。通过数学推导和代码实战,详细阐述了如何根据目标边界框尺寸动态计算椭圆长短轴半径,生成更贴合目标几何形状的热图监督信号,从而有效提升模型,尤其是对无人机、车辆等不规则目标的定位精度。

alice7model的博客 991

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

SpringBoot3 + OAuth2.1实战:5分钟搞定授权码模式(含完整代码)

本文详细介绍了如何在SpringBoot3环境中快速实现OAuth2.1的授权码模式,包括环境配置、核心安全实现及完整代码示例。通过Spring Security集成,开发者可以高效构建企业级安全认证系统,解决版本兼容性问题,提升开发效率。

weixin_29216353的博客 145

[LeetCode][10]Regular Expression Matching解析 -Java实现

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

一些奇奇怪怪的解法 2968

快速上手CH340N电路设计(CH340N USB转串口模块 USB Type-C接口 CH340系列芯片讲解)

一、上模块 二、功能分析 l 芯片:CH340N l 输入接口:USB、TYPE-C l 输出接口:TTL(5V\3.3V\GND\TX\RX) l 指示灯:电源、TX、RX 三、CH340x芯片 l CH340 是一个 USB 总线的转接芯片,实现 USB 转串口 l 全速 USB 设备接口,兼容 USB V2.0 l 硬件全双工串口,内置收发缓冲区,支持通讯波特率 50bps~2Mbps l 支持常用的 MODEM 联络信号 RTS、DTR、DCD、RI、DS...

东小东博客 5万+

【网络安全】零基础入门网络安全劝退指北

作为从16年接触网络安全的小白,谈谈零基础如何入门网络安全,有不对的地方,请多多指教。 这些年最后悔的事情莫过于没有把自己学习的东西积累下来形成一个知识体系。 后续我也会陆续的整理网络安全的相关学习资料及文章,与大家一起探讨学习。 如何入门 简单了解网络安全 网络安全就是指的确保网络系统中的数据不被别人破坏,而网安工程师就是涉及程序来维护网络安全。 网安方向有很多职位,比如安全产品工程师,安全分析师,数据恢复工程师,网络架构工程师,安全编程工程师,以及网络集成工程师等。 工作内容主要包括: 安全评测,风

互联网老辛 4610

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

022、Conv-BN-SiLU 基础卷积块:构造函数参数计算,前向传播的 PyTorch 逐行实现

本文详细解析了Conv-BN-SiLU卷积块的实现细节与注意事项。作者从调试经历出发,指出bias参数冗余会导致数值不稳定,强调卷积层应设置bias=False。文章对比了SiLU与ReLU的特性差异,逐行分析了构造函数参数计算(包括自动填充、分组卷积等)和前向传播过程,特别讲解了BN层与SiLU激活的数值特性。最后分享了三个实践中的常见问题:BN的momentum设置、SiLU输出范围的影响,以及分组卷积的通道对齐要求,并提供了卷积与BN融合的推理优化技巧。全文聚焦实际工程实现中的关键细节,具有较强实操指

ai技术增长的博客 226

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

buptlrw的专栏 2605

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

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

题目:Regular Expression Matching 正则表达式匹配 难度:困难 Given an input string (s) and a pattern (p), implement regular expression matching with support for'.'and'*'. '.' Matches any single character. '*' ...

qq_21963133的博客 551

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

leetcode 第10题 Regular Expression Matching

周末也没有做leetcode,感觉有点罪恶啊,这个第10题好难啊,涉及到递归了,看的我是头晕眼花啊。我看着看着还是有点收获的,我发现题目仍然没有我想象中的那么的简单,如果s = 'a b c d e',p = '. * e',这样匹配出的结果也是正确的,我就很纳闷儿啊,' . '是匹配任意一个字符,可是我以为只能匹配一个字符,但是从这个算例上看,当它和' * '在一起时,' . '可以多次匹配不同

paofuhemakalong的博客 989

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

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

bigfacesafdasgfewgf 1250

LEETCODE 10 Regular Expression Matching (JAVA题解)

LEETCODE 10 Regular Expression Matching (JAVA题解)

LCLSpring 1142

leetcode Regular Expression Matching

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

zyfo2的专栏 1128

LeetCode 10 .Regular Expression Matching【Java】

题目描述 正则表达式匹配 AC代码 dp[i][j]dp[i][j]dp[i][j]表示 s 的前 iii 个是否能被 ppp 的前 jjj 个匹配 class Solution { public boolean isMatch(String s,String p){ if (s == null || p == null) { ret...

NayelyA的博客 290

LeetCode-10. Regular Expression Matching [C++][Java]

Given an input stringsand a patternp, implement regular expression matching with support for'.'and'*'

贫道绝缘子的博客 956

LeetCode T10 Regular Expression Matching

题目地址: 中文:https://leetcode-cn.com/problems/regular-expression-matching/ 英文:https://leetcode.com/problems/regular-expression-matching/ 题目描述: Given an input string s and a pattern p, implement regular expression matching with support for ‘.’ and ‘*’ where: ‘.

之虎者也的博客 187

LeetCode10-Regular Expression Matching

LeetCode10-Regular Expression Matching

柚子的博客 600

Leetcode 10. Regular Expression Matching [hard][java]

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

inkhk的博客 263

【LeetCode】解题10:Regular Expression Matching(动态规划解法)

LeetCode解题 10:Regular Expression Matching(动态规划解法)Problem 10: Regular Expression Matching [Hard]解题思路Solution (Java) Problem 10: Regular Expression Matching [Hard] Given an input string (s) and a patter...

Fayedily的博客 418

[leetcode]010-Regular Expression Matching[递归]

1. 原题 https://leetcode.com/problems/regular-expression-matching/ 2. 思路 题意:按照给定的规则判断两个字符串是否匹配。题目有些难。 常规思路无法很好的处理。经分析,模式串与目标串的匹配可以层层分解成 子模式串与子目标串的匹配。于是,解决这类问题,可以用递归和动态规划。 递归容易理解,这里用的递归。 先处理递归边界。 ...

坚不萌 219
上一篇: LeetCode : Palindrome Number [java]
下一篇: LeetCode : Container With Most Water [java]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值