LeetCode: Regular Expression Matching

来源:互联网 发布:淘宝热卖怎么参加 编辑:程序博客网 时间:2024/04/27 21:25

似乎“.*”能匹配任意的字符串,如“abcd”


class Solution {

public:
    bool isMatch(const char *s, const char *p) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function    
        if(*p=='\0')
            return *s=='\0';
        if(*(p+1)!='*')
            return (*s==*p||(*p=='.'&&*s!='\0'))&&isMatch(s+1,p+1);
        while(*s==*p||(*p=='.'&&*s!='\0')){
            if(isMatch(s,p+2))
                return true;
            s++;
        }
        return isMatch(s,p+2);
    }
};