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(递归)的更多相关文章

  1. Leetcode 10. Regular Expression Matching(递归,dp)

    10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...

  2. [LeetCode] Regular Expression Matching 正则表达式匹配

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  3. [leetcode]Regular Expression Matching @ Python

    原题地址:https://oj.leetcode.com/problems/regular-expression-matching/ 题意: Implement regular expression ...

  4. LeetCode | Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  5. LeetCode——Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  6. [LeetCode] Regular Expression Matching [6]

    称号: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  7. LeetCode Regular Expression Matching 网上一个不错的实现(非递归)

    '.' Matches any single character.'*' Matches zero or more of the preceding element. The matching sho ...

  8. Leetcode:Regular Expression Matching分析和实现

    题目大意是要求我们实现一个简单的正则表达式全匹配判断.其中正则表达式中只包含一般字符,以及全匹配字符.和变长字符*.其中.可以匹配一个字符,而*与前一个字符相关联,x*可以被看作任意多个x(0到正无穷 ...

  9. LeetCode: Regular Expression Matching 解题报告

    Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...

随机推荐

  1. BZOJ3046 : lagoon

    码农题,拆点BFS预处理出所有联通块的面积即可,注意分类讨论. #include<cstdio> #include<cmath> using namespace std; co ...

  2. 【转】eclipse插件开发,调试运行,导出与安装

    [转自]http://www.kankanews.com/ICkengine/archives/61043.shtml 文章来自KENGINE | Kankanews.com 摘要: 本文主要讲ecl ...

  3. C++中inline这个玩意儿

    inline 说明这个函数是内联的,在编译过程中内联函数会直接被源代码替换,提高执行效率 如果类中的某个函数会被调用很多次或者放在循环中,那么建议将这个函数声明为内联,可以提高程序的运行效率

  4. POJ 1548 (二分图+最小路径覆盖)

    题目链接:http://poj.org/problem?id=1548 题目大意:给出一张地图上的垃圾,以及一堆机器人.每个机器人可以从左->右,上->下.走完就废.问最少派出多少个机器人 ...

  5. 【wikioi】1907 方格取数3(最大流+最大权闭合子图)

    http://www.wikioi.com/problem/1907/ 这题我一开始想到的是状压,看到n<=30果断放弃. 然后也想到了黑白染色,然后脑残了,没想到怎么连边. 很简单的一题 黑白 ...

  6. [BZOJ 2631]tree

    裸LCT..QAQ写了三遍没写对 真是老了..QAQ 主要错的地方是 init: size[i] = sum[i] = val[i] = mul[i] = 1; pushdown: 注意判断左右儿子是 ...

  7. JavaScript 导出Excel 代码

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

  8. Qt OpenCV Support Image Type 支持读写的图像格式

    Qt 支持的图片格式如下: Format Description Qt's support BMP Windows Bitmap Read/write GIF Graphic Interchange ...

  9. [转]Entity Framework走马观花之把握全局

    本文转自:http://blog.csdn.net/bitfan/article/details/12887007 Entity Framework走马观花 之 把握全局 ============== ...

  10. $.each(),$.map()归纳

    //$.each()对字典(没有索引).数组(有索引) 遍历 //两个参数 var json={"name":"李可","age":&quo ...