Regular Expression Matching
2017-08-05 11:04
253 查看
Implement regular expression matching with support for
/*
判断字符串是否匹配,其中要注意*表示前一个字母可以任意出现多次。
用动规就好了
*/
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.length(), n = p.length();
vector<vector<bool> > dp(m + 1, vector<bool> (n + 1, false));
dp[0][0] = true;
for (int i = 0; i <= m; i++)
for (int j = 1; j <= n; j++)
if (p[j - 1] == '*')
dp[i][j] = dp[i][j - 2] || (i > 0 && (s[i - 1] == p[j - 2] || p[j - 2] == '.') && dp[i - 1][j]);
else dp[i][j] = i > 0 && dp[i - 1][j - 1] && (s[i - 1] == p[j - 1] || p[j - 1] == '.');
return dp[m]
;
}
};
'.'and
'*'.
'.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") ? false isMatch("aa","aa") ? true isMatch("aaa","aa") ? false isMatch("aa", "a*") ? true isMatch("aa", ".*") ? true isMatch("ab", ".*") ? true isMatch("aab", "c*a*b") ? true
/*
判断字符串是否匹配,其中要注意*表示前一个字母可以任意出现多次。
用动规就好了
*/
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.length(), n = p.length();
vector<vector<bool> > dp(m + 1, vector<bool> (n + 1, false));
dp[0][0] = true;
for (int i = 0; i <= m; i++)
for (int j = 1; j <= n; j++)
if (p[j - 1] == '*')
dp[i][j] = dp[i][j - 2] || (i > 0 && (s[i - 1] == p[j - 2] || p[j - 2] == '.') && dp[i - 1][j]);
else dp[i][j] = i > 0 && dp[i - 1][j - 1] && (s[i - 1] == p[j - 1] || p[j - 1] == '.');
return dp[m]
;
}
};
相关文章推荐
- Regular Expression Matching ('.' and '*')
- 10Regular Expression Matching
- LeetCode (10): Regular Expression Matching [HARD]
- 10 Regular Expression Matching
- LeetCode OJ10 Regular Expression Matching
- LeetCode Regular Expression Matching
- leetcode 10. Regular Expression Matching
- leetcode.10---Regular Expression Matching
- Leetcode(10)Regular Expression Matching
- regular-expression-matching
- LeetCoder 10. Regular Expression Matching
- LeetCode 10. Regular Expression Matching
- Regular Expression Matching
- leetcode10_Regular Expression Matching
- [LeetCode] 079: Regular Expression Matching
- 正则表达式匹配-leetcode Regular Expression Matching
- regular expression matching --python
- [DP] LeetCode Regular Expression Matching
- [LeetCode] Regular Expression Matching - Recursively
- LeetCode 10. Regular Expression Matching