121_leetcode_Wildcard Matching

来源:互联网 发布:如何提升淘宝宝贝权重 编辑:程序博客网 时间:2024/06/05 15:32

Implement wildcard pattern matching with support for '?' and '*'.

'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover the entire input string (not partial).The function prototype should be:bool isMatch(const char *s, const char *p)Some examples:isMatch("aa","a") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "*") → trueisMatch("aa", "a*") → trueisMatch("ab", "?*") → trueisMatch("aab", "c*a*b") → false

1:注意特殊情况;2:遇到‘?',分别++;3:遇到‘*’的情况



    bool isMatch(const char *s, const char *p)    {        if(s == NULL || p == NULL)        {            return false;        }                bool flag = false;        const char* indexP = nullptr;        const char* indexS = nullptr;                while( *s != '\0')        {            if(*p == '?')            {                p++;                s++;            }            else if(*p == '*')            {                while(*p == '*')                {                    p++;                }                p--;                                indexP = p;                indexS = s;                flag = true;                p++;            }            else            {                if(*p == *s)                {                    p++;                    s++;                }                else                {                    if(flag == true)                    {                        p = indexP + 1;                        s = ++indexS;                    }                    else                    {                        return false;                    }                }            }        }                while(*p == '*')        {            p++;        }                if(*s == '\0' && *p == '\0')        {            return true;        }        else        {            return false;        }            }


0 0
原创粉丝点击