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
class Solution
{
public:
bool isMatch(const char *s, const char *p)
{
const char *sl = 0, *pl = 0;
while(*s != '\0')
{
if (*s == *p || '?' == *p)
{
s++;
p++;
}
else if ('*' == *p)
{
sl = s;
pl = p;
p++;
}
else if (pl != 0)
{
s = ++sl;
p = pl + 1;
}
else
return false;
}
while('*' == *p)
p++;
return '\0' == *p;
}
};回溯加减枝
本文介绍了一种实现通配符匹配的算法,该算法支持 '?' 和 '*' 两种通配符,其中 '?' 可以匹配任意单个字符,而 '*' 可以匹配任意长度的字符序列。文章提供了一个 C++ 实现的示例,并通过几个具体案例演示了如何使用该算法进行字符串匹配。
1041

被折叠的 条评论
为什么被折叠?



