[LeetCode] Regular Expression Matching [6]
称号:
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
解题思路
设计一个支持‘.' 和 '*' 的正則表達式匹配算法。
这个题复杂的地方在于对于 '*' 的处理。这个符号在正則表達式中被称为贪婪型的量词。这个量词在实际匹配过程中也是尽可能多的匹配直到词尾或者不匹配成功才结束。然后假设其后面还有没有匹配的,则回退到合适的位置。然后才进行下一个匹配。
正則表達式中的匹配优先与回溯大概也就是这个意思。关于正則表達式这方面的知识。有兴趣能够读读《精通正則表達式》的第4章表达式的匹配原理。
回到本题,正由于 '*'的特殊性。我们在分类的时候选择依据 '*' 来进行,分类后其子问题也是一个正則表達式匹配的问题。所以这能够使用递归来做。以下来看看代码,代码中有凝视说明匹配的类型:
代码实现:
class Solution {
public:
bool isMatch(const char *s, const char *p) {
if(s==NULL || p==NULL) return false;
if(*p == '\0') return *s=='\0';
if(*(p+1) != '*'){
if(*p==*s || (*p=='.' && *s!='\0'))
return isMatch(s+1, p+1);
return false;
}
else{
//s="aaaabbbb", p="a.*b"
while(*p==*s || (*p=='.' && *s!='\0')){
if(isMatch(s, p+2))
return true;
++s;
}
//s="ab", p="aa*b"
return isMatch(s, p+2);
}
}
};
另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3dhZ2xl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
)
版权声明:本文博主原创文章,博客,未经同意不得转载。
[LeetCode] Regular Expression Matching [6]的更多相关文章
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- LeetCode | Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- [leetcode]Regular Expression Matching @ Python
原题地址:https://oj.leetcode.com/problems/regular-expression-matching/ 题意: Implement regular expression ...
- [LeetCode] Regular Expression Matching(递归)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- LeetCode——Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- Leetcode:Regular Expression Matching分析和实现
题目大意是要求我们实现一个简单的正则表达式全匹配判断.其中正则表达式中只包含一般字符,以及全匹配字符.和变长字符*.其中.可以匹配一个字符,而*与前一个字符相关联,x*可以被看作任意多个x(0到正无穷 ...
- LeetCode Regular Expression Matching 网上一个不错的实现(非递归)
'.' Matches any single character.'*' Matches zero or more of the preceding element. The matching sho ...
- LeetCode: Regular Expression Matching 解题报告
Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...
- lc面试准备:Regular Expression Matching
1 题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...
随机推荐
- 解析汽车B2C商城网站四种盈利模式
汽车已成为家庭的日常用品,汽车的配套设施也成为销售的热点,汽车B2C电子商城为行业营销的新平台,汽车B2C电子商务网站盈利的模式是怎样的?创新的盈利模式才能在行业竞争中生存. 资讯产品一体模式 网站的 ...
- Linux 内核升级步骤
1.解压内核文件包#xz -d linux-3.2.63.tar.xz #tar xvf linux-3.2.63.tar 2.拷贝解压文件到/usr/src#cp -r linux-3.2.63 / ...
- Codeforces 452A Eevee
#include<bits/stdc++.h> using namespace std; string m[]={"vaporeon","jolteon&qu ...
- 启动、停止、重启 MySQL 常见的操作方法:
启动.停止.重启 MySQL 常见的操作方法: 简单罗列 一.启动方式 1.使用 service 启动:service mysqld start 2.使用 mysqld 脚本启动:/etc/inint ...
- xcode project
An Xcode project is a repository for all the files, resources, and information required to build one ...
- Redis key 设计技巧
1: 把表名转换为key前缀 如, tag: 2: 第2段放置用于区分区key的字段--对应mysql中的主键的列名,如userid 3: 第3段放置主键值,如2,3,4...., a , b ,c ...
- 腾讯视频QLV格式转换mp4的方法
腾讯视频QLV格式转换mp4的方法不知道大家知不知道用?喜欢用腾讯视频的朋友应该都知道腾讯视频单独搞出了个QLV格式文件,只能用腾讯独有的腾讯视频软件才能播放,就算用格式工厂转换也不行,那么腾讯视频的 ...
- 读书笔记:《为什么大猩猩比专家高明, How We Decide》
读书笔记:<为什么大猩猩比专家高明, How We Decide> 英文的书名叫<How We Decide>,可能是出版社的原因,非要弄一个古怪的中文书名<为什么大猩猩 ...
- POJ3050 Hopscotch 【DFS】
Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2113 Accepted: 1514 Descrip ...
- start_kernel——boot_init_stack_canary
/* * Initialize the stackprotector canary value. * * NOTE: this must only be called from functions t ...