leetcode 10. Regular Expression Matching(dp)

前端之家收集整理的这篇文章主要介绍了leetcode 10. Regular Expression Matching(dp)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

题目:https://leetcode.com/problems/regular-expression-matching/description/
题意:给你字符串s、p,让判断s是否符合p的正则匹配

‘.’ Matches any single character.
‘*’ Matches zero or more of the preceding element.
思路:令dp[i][j]代表s[0…i]和p[0…j]能否匹配,这里“*”难处理,代表0个字符和大于0个的情况
代码

class Solution {
public:
    bool dp[1005][1005];
    bool isMatch(string s,string p) {
        memset(dp,false,sizeof(dp));
        dp[0][0] = true;
        for(int i = 0;i <= s.length();i++){
            for(int j = 1;j <= p.length();j++){
                if(i >= 1 && s[i-1] == p[j-1] || p[j-1] == '.')
                    dp[i][j] = dp[i][j] || dp[i-1][j-1];
                if(p[j-1] == '*'){
                    if(i >= 1 && (s[i - 1] == p[j - 2] || p[j - 2] == '.'))//>0
                        dp[i][j] = dp[i][j] || dp[i - 1][j];
                    dp[i][j] =  dp[i][j] || dp[i][j - 2];//0
                }
            }
        }
        return dp[s.size()][p.size()];
    }
};
原文链接:https://www.f2er.com/regex/357901.html

猜你在找的正则表达式相关文章