类似剑指 Offer 19. 正则表达式匹配 | Cinte's Leetcode Record
dp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public: bool isMatch(string s, string p) { int length1 = s.size(), length2 = p.size(); vector<vector<int>> dp(length1 + 1, vector<int>(length2 + 1, false)); dp.back().back() = true; for(int i = length1; i >= 0; --i) { for(int j = length2 - 1; j >= 0; --j) { bool firstMatch = i < length1 && (s[i] == p[j] || p[j] == '?'); if(p[j] == '*') dp[i][j] = dp[i][j + 1] || (i < length1 && dp[i + 1][j]); else if(i < length1) dp[i][j] = firstMatch && dp[i + 1][j + 1]; } } return dp[0][0]; } };
|