题解 | #正则表达式匹配#

正则表达式匹配

http://www.nowcoder.com/practice/28970c15befb4ff3a264189087b99ad4


int match_core(char* str,char* pattern)
{
    if (*str == '\0' && *pattern == '\0')
        return 1;
    if (*str != '\0' && *pattern == '\0')
        return 0;
    if(*str == '\0' && *pattern != '\0')
    {
        if(pattern[1]=='*')
            return match_core(str, pattern+2);
        else
            return 0;
    }
    if (pattern[1] == '*')
    {
        if(pattern[0]==str[0]||pattern[0]=='.')
            return (match_core(str + 1, pattern)||match_core(str, pattern + 2));
        else
            return match_core(str, pattern+2);

    }
    else
    {
        if ((pattern[0] == '.') || pattern[0] == str[0])
            return match_core(str + 1, pattern + 1);
        else
            return 0;
    }

}



bool match(char* str, char* pattern ) {
    if (str == NULL || pattern == NULL)
        return 0;
    return match_core(str, pattern);
}
全部评论

相关推荐

冲芭芭拉鸭:你这图还挺新,偷了。
投递美团等公司10个岗位
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务