[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 ...
随机推荐
- 《深入理解OSGi:Equinox原理、应用与最佳实践》笔记_1_运行最简单的bundlehelloworld
<深入理解OSGi:Equinox原理.应用与最佳实践>笔记_1_运行最简单的bundlehelloworld 买了周大大的OSGI的书看 先前完全没有基础 就靠这本书看看学学 顺便记一些 ...
- 日期格式化标签<fmt:formatDate>&<fmt:setTimeZone>时区标签的使用demo
日期格式化标签<fmt:formatDate>&<fmt:setTimeZone>时区标签的使用demo <%@ page contentType="t ...
- Java字节流和字符流
file.txt文本中存储的内容: 好abc 1.字符流处理: package com.wjy.java; import java.io.FileInputStream; import java.io ...
- 阿里云server(ECS)优惠券领取
CoderMan的博客也是放置在阿里云的ECS上.速度绝对是刚刚的,大家打开的速度肯定不会慢. 有些同志们至今可能还在用虚拟主机吧,其实阿里云server真心不贵,有俩种计费方式:各自是按月计费和按流 ...
- iOS7 文本转语音 AVSpeechSynthesizer
OS7 的这个功能确实不错.我刚试了下,用官方提供的API ,简单的几句代码就能实现文本转语音! Xcode 5.0 工程建好后首先把AVFoundation.framework 加入到工程 AVSp ...
- ie6下margin双倍距的问题
今天中午休息时, 公司客服突然报出来一个bug, 一个用ie6的用户打开我们活动网站时, 发现内容都错乱了, 我赶紧上线一看, 发现是正常的. 找了台ie6的xp机器再看了下, 重现出了这个用户的问题 ...
- java 解析 json 遍历未知key
1.—————————————————————————————————————————————————————————————— import net.sf.json.JSONObject; Stri ...
- Linux中块设备驱动程序分析
基于<Linux设备驱动程序>书中的sbull程序以对Linux块设备驱动总结分析. 開始之前先来了解这个块设备中的核心数据结构: struct sbull_dev { i ...
- codeforces55D数位dp
codeforces55D 查询给定区间内的beautiful number. 一个数字是beautiful number当且仅当能被自己的各个数字不为0的位整除. 这个dp的状态还是挺难想的.一个 ...
- session校验是否登录
由于一个网站要有好多页面,如果每个页面都写上检验session是否为空,太麻烦了,所以写个工具类,就方便了. 1首先创建一个类库Common 2,然后在这个类库添加引用 3在Common继承 :Sys ...