class Solution {
// 必须掌握的一题
public:
bool isMatch(string s, string p) {
int start_s = -1,
start_p = -1,// 当p[start_p] =='*'时,s此时的索引为start_s
i = 0,
j = 0;
s += '\0'; //简化问题
p += '\0';
while(s[i] != '\0'){
if (s[i] == p[j] || p[j] == '\?'){
i++;
j++;
}
else if (p[j] == '*'){
start_s = i;
start_p = j++; // 跳过
}
else if (start_p != -1){ // 这一步非常关键,必须理解
//回溯
i = ++start_s;
j = start_p + 1;
}
else
return false;
}
while (p[j] != '\0' && p[j] == '*')
j++;
return p[j] == '\0';
}
};
leetcode 44 Wildcard Matching
最新推荐文章于 2020-12-28 15:59:41 发布