首先明确一点s中是没有 * 和 . 的,只有正常字符,很久才弄清楚这一点,不然这题很难理解
//s 待匹配字符串,只有普通的字符,没有*, .
//p 匹配模板
bool isMatch(const char *s, const char *p) {
- if (*p == '\0') return *s == '\0';
- // next char is not '*': must match current character
- if (*(p+1) != '*')
- return ((*p == *s) || (*p == '.' && *s != '\0')) && isMatch(s+1, p+1);
- // next char is '*'
- while ((*p == *s) || (*p == '.' && *s != '\0')) {
- if (isMatch(s, p+2)) return true;
- s++;
- }
- return isMatch(s, p+2);
}