当前位置:网站首页>Sword finger offer regular expression matching
Sword finger offer regular expression matching
2022-07-26 18:21:00 【Morita Rinko】
Regular Expression Matching
Subject requirements :
Please implement a function to match including ’.‘ and ’‘ Regular expression of .
1. Characters in pattern ’.‘ Represents any character
2. Characters in pattern ’' Indicates that the character in front of it can appear any number of times ( contain 0 Time ).
In the subject , Matching means that all characters of a string match the whole pattern . for example , character string "aaa" With the model "a.a" and "abaca" matching , But with the "aa.a" and "ab*a" No match 
Solution code :
class Solution {
public:
/**
* The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly
*
*
* @param str string character string
* @param pattern string character string
* @return bool Boolean type
*/
bool match(string str, string pattern) {
return MyMath(str, 0, pattern, 0);
}
// use i,j To indicate whether the subscript is better to judge whether it is out of bounds
bool MyMath(string str,int i,string patten,int j){
// If str Traverse complete ,patten If the traversal is just completed , Indicates that the match is successful
if(i>=str.size() && j==patten.size()){
return true;
}
// If str No traversal completed , however patten After traversal , Description matching failed
if(i<str.size() && j>=patten.size()){
return false;
}
//patten The suffix is *
if(patten[j+1]=='*' && (j+1)<patten.size()){
//str and patten If the match is successful
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;
}
};
边栏推荐
- Ten year structure five year life-06 impulse to leave
- openssl
- 1、 Header file, output format,::, namespace
- Leetcode 50 day question brushing plan (day 2 - the longest substring without repeated characters 10.00-12.00)
- AI sky covering DL multilayer perceptron
- quartz触发器规则
- What is the PMP exam outline in 2022?
- 贪心——455. 分发饼干
- Several ways to resolve hash conflicts
- 推荐效果不如意,不如试试飞桨图学习
猜你喜欢

236. The nearest common ancestor of a binary tree

剑指offer 连续子数组的最大和(二)

推荐效果不如意,不如试试飞桨图学习

【数字IC】深入浅出理解AXI-Lite协议

Vector CANoe Menu Plugin拓展入门

It is said that the salary of Alibaba P7 is really fragrant
![[static code quality analysis tool] Shanghai daoning brings you sonarource/sonarqube download, trial and tutorial](/img/09/209a405953d99d7d8b347c01873eba.png)
[static code quality analysis tool] Shanghai daoning brings you sonarource/sonarqube download, trial and tutorial

Hosts this file has been set to read-only solution

数据仓库:详解维度建模之事实表

Become a test / development programmer, Xiao Zhang: reality is coming
随机推荐
链表-合并两个排序的列表
8.2 一些代数知识(群、循环群和子群)
[day3] reconstruction of roads
[training Day2] sculpture
9、 Alternative implementation of client for service communication
Three ways of de duplication in SQL
[unity3d] rocker
10、 Implementation of parameter modification of parameter server
6、 Common commands of ROS (I): rosnode, rostopic, rosmsg
[training Day2] torchbearer
贪心——455. 分发饼干
Detailed explanation of openwrt's feeds.conf.default
PS_ 1_ Know the main interface_ New document (resolution)_ Open save (sequence animation)
Relative path and absolute path
2020美亚个人赛复盘
202. Happy number
网易游戏研发工程师实习生(客户端方向)一面
The user experience center of Analysys Qianfan bank was established to help upgrade the user experience of the banking industry
Spark data format unsafe row
Hosts this file has been set to read-only solution