说是动态规划,实际上是一道回溯暴力搜索。
注意一点就可以了。
*的数量可以是0、1、2、3、4.我们只要遍历出当*是0的时候,后续的组合是不是符合要求的。这样我们才可以满足1之后的情形
bool isMatch(char* s, char* p) {
if (s == NULL || p == NULL) return false;
if (*p == '\0') return *s == '\0';
// ".*" matches "", so we can't check (*s == '\0') here.
if (*(p + 1) == '*')
{
// Here *p != '\0', so this condition equals with
// (*s != '\0' && (*p == '.' || *s == *p)).
while ((*s != '\0' && *p == '.') || *s == *p)
{
if (isMatch(s, p + 2)) return true;
++s;
}
return isMatch(s, p + 2);
}
else if ((*s != '\0' && *p == '.') || *s == *p)
{
return isMatch(s + 1, p + 1);
}
return false;
}