leetcode10-Regular Expression Matching

1,官方C++递归:

class Solution {
public:
    bool isMatch(string s, string p) {
        if (p.empty()) return s.empty();
        if (p.size() > 1 && p[1] == '*') {
            return isMatch(s, p.substr(2)) || (!s.empty() && (s[0] == p[0] || p[0] == '.') && isMatch(s.substr(1), p));
        } else {
            return !s.empty() && (s[0] == p[0] || p[0] == '.') && isMatch(s.substr(1), p.substr(1));
        }
    }
};

2,官方java递归:

class Solution {
    public boolean isMatch(String text, String pattern) {
        if (pattern.isEmpty()) return text.isEmpty();
        boolean first_match = (!text.isEmpty() &&
                               (pattern.charAt(0) == text.charAt(0) || pattern.charAt(0) == '.'));

        if (pattern.length() >= 2 && pattern.charAt(1) == '*'){
            return (isMatch(text, pattern.substring(2)) ||
                    (first_match && isMatch(text.substring(1), pattern)));//当长度大于等于2且第二个是*符号时候,分两种情况讨论:1,
//*表示零个前面元素2,*表示非另个前面元素。
 } else {
            return first_match && isMatch(text.substring(1), pattern.substring(1));
        }
    }

}

3,官方python递归:

class Solution(object):
    def isMatch(self, text, pattern):
        if not pattern:
            return not text

        first_match = bool(text) and pattern[0] in {text[0], '.'}

        if len(pattern) >= 2 and pattern[1] == '*':
            return (self.isMatch(text, pattern[2:]) or
                    first_match and self.isMatch(text[1:], pattern))
        else:
            return first_match and self.isMatch(text[1:], pattern[1:])

4,自己写的递归C++:

class Solution {
public:
    bool isMatch(string s, string p) {
        if (p.empty())
            return s.empty();
        bool begin=!s.empty()&&(s[0]==p[0]||p[0]=='.');//开始这个s是否为空忘记了,,以后只要用到数组就要注意是否超出范围。。
        if (p.size()>=2&&p[1]=='*')
            return isMatch(s,p.substr(2))||(begin&&isMatch(s.substr(1),p));
        else
            return begin&&isMatch(s.substr(1),p.substr(1));
    }
};

5,自己码的dp解法C++:

class Solution {
public:
    bool isMatch(string s, string p) {
        int m=s.size(),n=p.size();
        vector<vector<bool>> dp(m+1,vector<bool>(n+1,false));//第一次错误:vector<vector>-------->改成:vector<vector<bool>>,后面那个
        //vector<bool>(n+1,false)相当于类名(构造函数)形成临时对象赋值
        dp[0][0]=true;
        
        for( int i=0;i<m+1;++i)//第二次错误:dp[0][2]不一定等于零,当p[1]=*的时候等于1,所以i要从0开始
            for(int j=1;j<n+1;++j)
            {
                if(j>1&&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]);
                    //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]&&(p[j-1]==s[i-1]||p[j-1]=='.');
                   // dp[i][j] = i > 0 && dp[i - 1][j - 1] && (s[i - 1] == p[j - 1] || p[j - 1] == '.');
            }
        return dp[m][n];
    }
};

收获:

1,dp看问题求解时,必须先要知道i-1,从而迭代for循环i和j要从小值开始;

2,数组使用时必须保证能用得到,从而用i>1;

猜你喜欢

转载自blog.csdn.net/u011776818/article/details/80972999