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") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false
给定一个字符串s和一个模式串p,判断模式串p和字符串s是否匹配
"?"能匹配任意单个字符串
"*"能匹配一个任意长度的字符串,包括空串
关键在于确定每个*匹配的字符串长度。
class Solution {
public:
bool isMatch(const char *s, const char *p) {
const char *sStart="\0";
const char *pStart="\0";
while(*s){
if(*p=='*'){
while(*p=='*')p++;
if(*p=='\0')return true;
sStart=s;
pStart=p;
}
else if(*p=='?'){
s++;p++;
}
else if(*p!='\0'){
if(*s==*p){
s++;p++;
}
else{
//如果对应位置上的字符不匹配
//则我们认为和p当前字符匹配的字符还在被匹配串的后边位置,则最近的那个*需要再多匹配一些字符。
//因此需要回溯,并增加*匹配的字符数,一般就+1
//如果发现前面没有出现过*, 也就无法回溯,只能认命匹配不了,返回false
if(!*sStart || !*pStart)return false;
s=++sStart;
p=pStart;
}
}
else{
//如果模式串已经结束了,但是被匹配串的还没匹配完。
//则,我们需要考虑最近的那个*需要再多匹配一些字符。
//因此需要回溯,并增加*匹配的字符数,一般就+1
//如果发现前面没有出现过*, 也就无法回溯,只能认命匹配不了,返回false
if(!*sStart || !*pStart)return false;
s=++sStart;
p=pStart;
}
}
//别忘了模式串最后可能还剩点尾巴
while(*p){
if(*p!='*')return false;
p++;
}
return true;
}
};LeetCode: Wildcard Matching [043],布布扣,bubuko.com
LeetCode: Wildcard Matching [043]
原文:http://blog.csdn.net/harryhuang1990/article/details/26506539