LeetCode 10. 正则表达式匹配 Regular Expression Matching 《剑指offer》第十九题:正则表达式匹配

class Solution {
public:
    bool isMatch(string s, string p) {
        return isMatchCore(s.c_str(), p.c_str());
    }
    
    bool isMatchCore(const char* s, const char* p)
    {
        if (*s == '\0' && *p == '\0') return true;
        if (*s != '\0' && *p == '\0') return false;
        bool first_match = *s && (*s == *p || *p == '.');
        
        if (*(p + 1) == '*')  //下一位是*
            return isMatchCore(s, p + 2)  //当前模式重复0次
             || (first_match && isMatchCore(s + 1, p));  //当前字符重复
        else
            return first_match && isMatchCore(s + 1, p + 1);
    }
};

 类似题目:《剑指offer》第十九题:正则表达式匹配

猜你喜欢

转载自www.cnblogs.com/ZSY-blog/p/12951308.html