30.正则匹配

思路:https://blog.csdn.net/qq_28410301/article/details/100182901

class Solution {
public:
    bool isMatch(string str, string pattern) {
        //递归结束
        if(str.empty() && pattern.empty())
            return true;
        
        //查看首元素是否一致
         bool first_match = !str.empty() && (str[0] == pattern[0] || pattern[0] == '.');
        
        //如果下一个字符带"*"
        if(pattern.size() >= 2 && pattern[1] == '*')
            return (bool) (isMatch(str,pattern.substr(2)) || (first_match && isMatch(str.substr(1),pattern)));
        else{
            return bool(first_match && isMatch(str.substr(1),pattern.substr(1)));
        }        
        
    }
};

猜你喜欢

转载自www.cnblogs.com/make-big-money/p/12508372.html