LeetCode 010 Regular Expression Matching
2024-08-28 20:36:32
题目描述: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 (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
代码如下:
class Solution {
public:
bool isMatch(const char *s, const char *p) { if(*p == '\0') return *s == '\0'; //如果下一个字符不是*,那么就需要匹配当前的字符!
if(*(p + 1) != '*'){ //下一个字符相等,或者有一个是'.',则匹配成功
if(*p == *s || (*p == '.' && *s != '\0'))
return isMatch(s + 1, p + 1);
else
return false;
} //下一个字符是*,则当前字符可以为0个或多个!
else{
while(*p == *s || (*p == '.' && *s != '\0')){
if(isMatch(s, p + 2))
return true;
s++;
}
return isMatch(s, p + 2);
} }
};
最新文章
- .NET平台机器学习资源汇总,有你想要的么?
- Odoo domain 中的 like, ilike, =like, =ilike 举例说明【转】
- Java面向对象思想解决猜拳问题
- curl get post 数据
- NopCommerce 3.80框架研究(一) 数据访问与持久化
- 从malloc中窥探Linux内存分配策略
- 改变UIView 的位置 Center和Frame
- MySQL原生HA方案 – Fabric体验之旅
- hadoop2.2.0伪分布模式64位安装
- bash的元字符(下)
- Spring笔记 - Bean xml装配
- org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.entity.annotations.House.district in
- Mycat 分片规则详解--枚举分片
- 从零开始学TensorFlow
- JavaScript的文档对象模型DOM
- Vue : Expected the Promise rejection reason to be an Error
- 手机与PC的影音相互播放(DLNA/UPNP)
- tomcat-maven-plugin的使用
- JS模拟下拉框select
- 学习POC框架pocsuite--编写hellowordPOC