题目:
题解:
bool isMatch(char* s, char* p) {int m = strlen(s);int n = strlen(p);// dp[i][j] 表示 s 的前 i 个字符和 p 的前 j 个字符是否匹配bool dp[m + 1][n + 1];memset(dp, false, sizeof(dp));dp[0][0] = true; // 空字符串和空模式匹配// 处理 * 匹配空字符的情况for (int j = 1; j <= n; j++) {if (p[j - 1] == '*') {dp[0][j] = dp[0][j - 2];}}// 填充 dp 数组for (int i = 1; i <= m; i++) {for (int j = 1; j <= n; j++) {if (p[j - 1] == s[i - 1] || p[j - 1] == '.') {dp[i][j] = dp[i - 1][j - 1];} else if (p[j - 1] == '*') {dp[i][j] = dp[i][j - 2] || (dp[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.'));}}}return dp[m][n];
}