'?' 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
和上一个算乘法一样,平时怎么找,现在怎么找。 正常人寻找pattern,一个一个对上, 如果碰到*,那么看*后面的对上不,如果后面只对上后面一部分,那么没对上的默认为*
首先比较 ? 或者相等,如果是,两个ptr都advance,
如果有星号,记下当前星号位置,记下当前被找的位置重新搜。
如果都失败了而且前面有星号,默认刚才记住的位置被*覆盖,向前移动一位,然后用星号后面的重新match.直到找完全部目标
然后循环p,看后面是不是都是*,如果不是那么一样match不上
#include <iostream>
using namespace std;
class Solution {
public:
bool isMatch(char *s, const char* p) {
const char* star = 0;
char* ss = s;
if (*p == '\0')
return *p == *s;
while (*s) {
if (*p == '?' || *p == *s) {
p++;
s++;
continue;
}
if (*p == '*') {
star = p++;
ss = s;
continue;
}
if (star) {
p = star + 1;
s = ++ss;
continue;
}
return false;
}
while (*p == '*')
p++;
return !*p;
}
};
int main() {
char* s = "abcd";
const char* p = "a*d";
Solution ss;
cout << ss.isMatch(s, p) << endl;
return 0;
}