当前位置:网站首页>力扣——10. 正则表达式匹配
力扣——10. 正则表达式匹配
2022-07-27 10:36:00 【weixin_54096215】
1.题目
2.思路
首先字符串s.length()>0,或者判断s.isEmpty();
可以分两步走,第一部分主要讨论第一个正则符号的可能性,利用&&,||表达式
情况1:可能是字符,那就看第二个符号;pattern.charAt(0)==text.charAt(0);
情况2:如果是".",则也需要看第二个字符;pattern.charAt(0)=='.'
第二部分就是考虑第二个字符的可能性。(pattern.length()>2)
情况1:如果第二个字符是“*”(pattern.charAt(1)=='*'),则判断第一个正则表达式的字符到底是.还是字符。利用isMatch函数进行匹配first_match && isMatch(text.substring(1),pattern)
情况2:如果不是“*”,再看是否和text的文本匹配first_match&&isMatch(pattern.substring(1),text.substring(1))
3.代码
public class Solution {
public boolean isMatch(String text, String pattern) {
if(pattern.isEmpty()) return text.isEmpty();
boolean first_match=(!text.isEmpty() &&(pattern.charAt(0) == text.charAt(0) || pattern.charAt(0)=='.'));
if(pattern.length()>=2 && pattern.charAt(1)=='*'){
return (isMatch(text,pattern.substring(2))||
(first_match && isMatch(text.substring(1),pattern)));
}
else{
return first_match&&isMatch(pattern.substring(1),text.substring(1));
}
}
}边栏推荐
- 十年架构五年生活-07 年轻气盛的蜕变
- properties文件
- PAT(乙级)2022年夏季考试
- 求组合数 AcWing 885. 求组合数 I
- What changes will metauniverse bring to the music industry in the trillion market?
- 博弈论 AcWing 893. 集合-Nim游戏
- Ten year structure five year life-07 young and vigorous transformation
- 神经网络学习笔记
- 01 BTC cryptology principle
- Learning notes - wechat payment
猜你喜欢

图片中非0值的数量对分类的影响

NFT leaderboard -nft real offer latest address: NFT leaderboard.com

Antd table+checkbox default value display

Use of pyquery

最长上升子序列模型 AcWing 1016. 最大上升子序列和

熵与形态的非递进现象

15 design movie rental system

中国剩余定理 AcWing 204. 表达整数的奇怪方式

The longest ascending subsequence model acwing 1016. The sum of the largest ascending subsequence

Internal and external troubles of digital collection NFT "boring ape" bayc
随机推荐
tensorflow运行报错解决方法
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
数字三角形模型 AcWing 1015. 摘花生
parsel的使用
学习笔记-简易服务器实现
The difference of iteration number and information entropy
Maximized array sum after 13 K negations
求组合数 AcWing 886. 求组合数 II
Internal and external troubles of digital collection NFT "boring ape" bayc
How to assemble a registry
The article will not keep VIP charges all the time. It will be open for a period of time
Students, don't copy all my code, remember to change it, or we both want G
12 is at least twice the maximum number of other numbers
迭代次数的差异与信息熵
SQL Server2000数据库错误
Today's code farmer girl learned notes about event operation and ref attribute, and did the practice of form two-way binding
求组合数 AcWing 885. 求组合数 I
2022 Niuke multi school training (3) a-ancestor topic translation
NFT leaderboard -nft real offer latest address: NFT leaderboard.com
Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.