regular-expression-matching

https://leetcode.com/problems/regular-expression-matching/express

 

我以爲這個遞歸解法很好,簡潔明瞭:spa

public:
    bool isMatch(string s, string p) {
        if (p.empty())    return s.empty();
        
        if ('*' == p[1])
            // x* matches empty string or at least one character: x* -> xx*
            // *s is to ensure s is non-empty
            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));
    }
};

 

也能夠方便的改爲DP.code

本站公眾號
   歡迎關注本站公眾號,獲取更多信息