天天看点

剑指offer——正则表达式

class Solution {
public:
    bool match(char* str, char* pattern)
    {
        if(str==nullptr||pattern==nullptr)
            return false;
        return matchcore(str,pattern);
    }
    bool matchcore(char* str,char* pattern){
        if(*str=='\0'&&(*pattern=='\0'))
            return true;
        if(*str!='\0'&&(*pattern=='\0'))
            return false;
        if(*(pattern+1)=='*'){
            if(*pattern==*str||(*pattern=='.'&&(*str!='\0')))//分三种情况
                return matchcore(str+1,pattern+2)||matchcore(str,pattern+2)||matchcore(str+1,pattern);
            else
                return matchcore(str,pattern+2);
        }
        if(*str==*pattern||(*pattern=='.'&&(*str!='\0')))
            return matchcore(str+1,pattern+1);
        return false;//其他情况,不匹配
    }
};