Wildcard Matching

来源:互联网 发布:数据开放平台浏览器 编辑:程序博客网 时间:2024/05/21 17:48

http://discuss.leetcode.com/questions/222/wildcard-matching

解法一

class Solution {public:    bool isMatch(const char *s, const char *p) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        if (*p == '*'){//return true;            while(*p == '*') ++p;            if (*p == '\0') return true;            while(*s != '\0' && !isMatch(s,p)){                ++s;                            }            return *s != '\0';        }        else if (*p == '\0' || *s == '\0') return *p == *s;        else if (*p == *s || *p == '?') return isMatch(++s,++p);        else return false;    }};


 

解法二

class Solution {public:bool isMatch(const char *s, const char *p) {    // Start typing your C/C++ solution below    // DO NOT write int main() function    if(!s && !p) return true;    const char *star_p=NULL,*star_s=NULL;    while(*s)    {        if(*p == '?' || *p == *s)        {            ++p,++s;        }else if(*p == '*')        {            //skip all continuous '*'            while(*p == '*') ++p;            if(!*p) return true; //if end with '*', its match.            star_p = p; //store '*' pos for string and pattern            star_s = s;        }else if((!*p || *p != *s)  && star_p)        {            s = ++star_s; //skip non-match char of string, regard it matched in '*'            p = star_p; //pattern backtrace to later char of '*'        }else            return false;    }    //check if later part of p are all '*'    while(*p)        if(*p++ != '*')            return false;    return true;}};


 

原创粉丝点击