当前位置:网站首页>剑指offer 正则表达式匹配
剑指offer 正则表达式匹配
2022-07-26 17:31:00 【早田凛凛子】
正则表达式匹配
题目要求:
请实现一个函数用来匹配包括’.‘和’‘的正则表达式。
1.模式中的字符’.‘表示任意一个字符
2.模式中的字符’'表示它前面的字符可以出现任意次(包含0次)。
在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"abaca"匹配,但是与"aa.a"和"ab*a"均不匹配
解题代码:
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param str string字符串
* @param pattern string字符串
* @return bool布尔型
*/
bool match(string str, string pattern) {
return MyMath(str, 0, pattern, 0);
}
//用i,j来表示下标更好的判断是否越界
bool MyMath(string str,int i,string patten,int j){
//如果str遍历完成,patten也正好遍历完成的话,说明匹配成功
if(i>=str.size() && j==patten.size()){
return true;
}
//如果str没有遍历完成,但是patten遍历完成的话,说明匹配失败
if(i<str.size() && j>=patten.size()){
return false;
}
//patten后缀为*
if(patten[j+1]=='*' && (j+1)<patten.size()){
//str和patten匹配成功的话
if(i<str.size() && (str[i]==patten[j] || patten[j]=='.')){
return (MyMath(str, i+1, patten, j) || MyMath(str, i, patten, j+2));
}else{
return MyMath(str, i, patten, j+2);
}
}
if((str[i]==patten[j] || patten[j]=='.') && i<str.size()){
return MyMath(str, i+1, patten, j+1);
}
return false;
}
};
边栏推荐
- Become a test / development programmer, Xiao Zhang: reality is coming
- Three ways of de duplication in SQL
- Sign up now | oar hacker marathon phase III midsummer debut, waiting for you to challenge
- SQL determines whether a column contains Chinese characters, English characters, pure numbers, and data interception
- How to switch nodejs versions at will?
- Win10 连接无线不能输入密码字符,一输入就卡死
- 常用api
- Common APIs
- Hosts this file has been set to read-only solution
- 相对路径与绝对路径
猜你喜欢
随机推荐
ssm练习第三天_分页助手_安全框架
俄语翻译的就业前景怎样 如何做好俄语翻译工作
ssm练习第四天_获取用户名_用户退出_用户crud_密码加密_角色_权限
SSM练习第五天
Come on developer! Not only for the 200000 bonus, try the best "building blocks" for a brainstorming!
[static code quality analysis tool] Shanghai daoning brings you sonarource/sonarqube download, trial and tutorial
The database uses PSQL and JDBC to connect remotely and disconnect automatically from time to time
AI zhetianchuan DL regression and classification
[day3] reconstruction of roads
2022 Henan Mengxin League game (3): Henan University
Simple uploading and downloading of Web project files
8.2 some algebraic knowledge (groups, cyclic groups and subgroups)
AI遮天传 ML-集成学习
AI遮天传 DL-多层感知机
SQL判断某列中是否包含中文字符、英文字符、纯数字,数据截取
PMP考生必读,7月30日考试防疫要求都在这里
Win10 wireless connection cannot input password characters, and it will be stuck as soon as it is input
LeetCode 0139. 单词拆分
菜鸟 CPaaS 平台微服务治理实践
4、 Service communication principle, code implementation









![[unity3d] rocker](/img/b7/40643a2676b251c185ce58840f7581.png)