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

思路是分别处理a*、.*、.、a的情况。

三周前写的lengthy code如下:

 class Solution {
public:
bool isMatch(const char *s, const char *p) {
int sn = strlen(s);
int pn = strlen(p);
return recursive(s, sn, p, pn);
} bool recursive(const char* s, int sn, const char* p, int pn) {
if (sn == && pn == ) return true;
if (pn == ) return false; if (*(p + ) != '\0') {
if (*(p + ) == '*') {
if (*p == '.') { // .*
int n = ;
while (n <= sn) {
if (recursive(s + n, sn - n, p + , pn - )) return true;
n++;
}
} else { // a*
int n = ;
while (n <= sn && *(s + n) == *p) {
if (recursive(s + n, sn - n, p + , pn - )) return true;
n++;
}
if (recursive(s + n, sn - n, p + , pn - )) return true;
}
} else {
if (*p != '.' && *s != *p) return false;
if (recursive(s + , sn - , p + , pn - )) return true;
}
} else {
if (*p != '.' && *s != *p) return false;
if (recursive(s + , sn - , p + , pn - )) return true;
}
}
};

今天看了Leetcode上1337的代码真是羞愧啊。http://leetcode.com/2011/09/regular-expression-matching.html

重写了一遍。思路还是一样。

 class Solution {
public:
bool isMatch(const char *s, const char *p) {
if (*p == '\0') return (*s == '\0');
// match single '\0', '.', 'a'...
if (*(p + ) != '*') {
return ((*s == *p || (*p == '.' && *s != '\0')) && isMatch(s + , p + ));
} // match a*, .*
while ((*s == *p || (*p == '.' && *s != '\0'))) {
if (isMatch(s++, p + )) return true;
} // ignore a*, *p != '.' && *s != *p
return isMatch(s, p + );
}
};

LeetCode | Regular Expression Matching的更多相关文章

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

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

  2. [leetcode]Regular Expression Matching @ Python

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

  3. [LeetCode] Regular Expression Matching(递归)

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

  4. [LeetCode] Regular Expression Matching [6]

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

  5. LeetCode——Regular Expression Matching

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

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

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

  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 解题报告

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

  9. lc面试准备:Regular Expression Matching

    1 题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...

随机推荐

  1. DP:Cow Bowling(POJ 3176)

    北大教你怎么打保龄球 题目很简单的,我就不翻译了,简单来说就是储存每一行的总数,类似于状态压缩 #include <stdio.h> #include <stdlib.h> # ...

  2. codeforces B. Flag Day 解题报告

    题目链接:http://codeforces.com/problemset/problem/357/B 题目意思:输入n个人和m场舞蹈,给出每场舞蹈(只有3个人参与)中参与的舞者的编号,你需要为这些舞 ...

  3. HDU 1521 排列组合 指数型母函数

    排列组合 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status D ...

  4. July 14th, Week 29th Thursday, 2016

    Risk comes from not knowing what you are doing. 风险常常来自于不知道自己在做什么. What is risk? I think risk means t ...

  5. 解决reload AVD list: cvc-enumeration-valid: Value '360dpi' is not facet-valid with respect to enumeration '[ldpi, mdpi, tvdpi, hdpi, 280dpi, xhdpi, 400dpi, xxhdpi, 560dpi, xxxhdpi]'. It must be a v

    解法: 将 D:\work\android-sdk-windows\tools\lib\devices.xml 替换到 D:\work\android-sdk-windows\system-image ...

  6. 【读书笔记】读《JavaScript设计模式》之适配器模式

    一.定义 适配器模式可用来在现有接口和不兼容的类之间进行匹配.使用这种模式的对象又叫包装器(wrapper),因为它们是在用一个新的接口包装另一个对象.在设计类的时候旺旺会遇到有些接口不能与现有API ...

  7. decltype

    在C++中,decltype作为操作符,用于查询表达式的数据类型.decltype在C++11标准制定时引入,主要是为泛型编程而设计,以解决泛型编程中,由于有些类型由模板参数决定,而难以(甚至不可能) ...

  8. UML从需求到实现----用例

    关于用例图的概念相信不用我去说了 .能看到这篇文章的都是知道用例图概念的人. UML 中最重要的是什么图呢 ?毫无疑问应该是用例图 ,用例是后期时序图 和实际开发的重要依据. 说明一下用例图是怎么产生 ...

  9. BZOJ 1192: [HNOI2006]鬼谷子的钱袋 数学结论

    1192: [HNOI2006]鬼谷子的钱袋 Description 鬼谷子非常聪明,正因为这样,他非常繁忙,经常有各诸侯车的特派员前来向他咨询时政.有一天,他在咸阳游历的时候,朋友告诉他在咸阳最大的 ...

  10. URI和URL的区别

    这两天在写代码的时候,由于涉及到资源的位置,因此,需要在Java Bean中定义一些字段,用来表示资源的位置,比如:imgUrl,logoUri等等.但是,每次定义的时候,心里都很纠结,是该用imgU ...