[LeetCode] 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'; //empty if (*(p + ) != '*') {//without *
if(!matchFirst(s,p))
return false;
return isMatch(s + , p + );
} else { //next: with a *
if(isMatch(s, p + ))
return true; //try the length of 0
while ( matchFirst(s,p) ) //try all possible lengths
if (isMatch(++s, p + ))
return true;
}
}
private:
bool matchFirst(const char *s, const char *p){
return (*p == *s || (*p == '.' && *s != '\0'));
}
};
[LeetCode] Regular Expression Matching(递归)的更多相关文章
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [leetcode]Regular Expression Matching @ Python
原题地址:https://oj.leetcode.com/problems/regular-expression-matching/ 题意: Implement regular expression ...
- LeetCode | Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- LeetCode——Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [LeetCode] Regular Expression Matching [6]
称号: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- LeetCode Regular Expression Matching 网上一个不错的实现(非递归)
'.' Matches any single character.'*' Matches zero or more of the preceding element. The matching sho ...
- Leetcode:Regular Expression Matching分析和实现
题目大意是要求我们实现一个简单的正则表达式全匹配判断.其中正则表达式中只包含一般字符,以及全匹配字符.和变长字符*.其中.可以匹配一个字符,而*与前一个字符相关联,x*可以被看作任意多个x(0到正无穷 ...
- LeetCode: Regular Expression Matching 解题报告
Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...
随机推荐
- emacs yasnippet
首先安装emacs 然后下载yasnippet-bundle-0.6.1c.el.tgz解压 在~/.emacs.d/文件夹下新建一个文件plug,一般是新建一个plugins但是我到下面有这个文件夹 ...
- POJ 2955 (区间DP)
题目链接: http://poj.org/problem?id=2955 题目大意:括号匹配.对称的括号匹配数量+2.问最大匹配数. 解题思路: 看起来像个区间问题. DP边界:无.区间间隔为0时,默 ...
- HDU 4419 Colourful Rectangle(线段树+扫描线)
题目链接 主要是pushup的代码,其他和区间更新+扫描线差不多. 那个区间如果要再刷一层x,那么sum[x][rt] = que[r+1] - que[l];但是如果原本有颜色为i,颜色将会变成i| ...
- c++ map 的使用
1.map是一类关联式容器,它是模板类. 关联的本质在于元素的值与某个特定的键相关联,而并非通过元素在数组中的位置类获取.它的特点是增加和删除节点对迭代器的影响很小,除了操作节点,对其他的节点都没有什 ...
- Qt5.4 VS2010 Additional Dependancies
Go to Linker -> General -> Additional LIbrary Directories: qtmaind.libQt5Cored.libQt5Guid.libQ ...
- ArcEngine 不能再打开其他表了
在IFeatureClass.Search()是弹出这个问题,根据网上的资料,采用 System.Runtime.InteropServices.Marshal.ReleaseComObject()或 ...
- [文字雲產生器] Tagxedo 把文字串成雲、變成畫,印在 T-Shirt、馬克杯、詩袋….
http://www.tagxedo.com/app.html 有種東西叫「Word Clouds」,就是把一堆文字依照不同的大小.顏色.角度與位置拼湊在一起,讓他變成像一朵雲一般.組合成各種不同的形 ...
- Editplus从下载到使用
☆ 准备工作 1,保证浏览器正常上网 2,能下载软件或已经下载到Editplus这个工具. ☆ 下载editplus 在浏览器输入http://www.editplus.com,然后回车.进入edit ...
- QQ、淘宝、阿里旺旺在线网页链接代码及详解 很实用
你可直接到官网去生成代码,简单.方便,相信都能上网的你,对这不会有难度的,认识字的就行,赶紧去吧! 1.阿里旺旺官网: http://page.1688.com/html/wangwang/dow ...
- 2016.05.03,英语,《Vocabulary Builder》Unit 21
sub, means 'under', as in subway, submarine, substandard. A subject is a person who is under the aut ...