leetcode这道题还挺有意思的,实现通配符,'?'匹配任意字符,'*'匹配任意长度字符串,晚上尝试了一下,题目如下:
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
直接的思路很简单,两个字符串从头开始匹配,不匹配直接返回false,匹配,则两个指针都加1,匹配子字符串,所以自然而然就想用递归来实现。
复杂一点的是*号,匹配任意长度的字符串,所以*的判断也可以循环的递归isMatch判断*后的子字符串。实现如下:
class Solution { public: bool isMatch(const char *s, const char *p) { if ('