LeetCode Wildcard Matching
2015-03-06 10:41
267 查看
Implement wildcard pattern matching with support for
题意:匹配两个字符串,?代表一个字符,*代表一个任意序列。
思路:贪心去做,重点是如果匹配"*",我们需要记录他的位置为start,和此时s串的位置tmp,然后对于p的下一位,我们是希望它能够匹配的,但是如果无法匹配的话,那么此时就需要用到我们标记的"*"的位置了,让"*"去多匹配一个字符,直到“*”的下一位能都匹配成功。
'?'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
题意:匹配两个字符串,?代表一个字符,*代表一个任意序列。
思路:贪心去做,重点是如果匹配"*",我们需要记录他的位置为start,和此时s串的位置tmp,然后对于p的下一位,我们是希望它能够匹配的,但是如果无法匹配的话,那么此时就需要用到我们标记的"*"的位置了,让"*"去多匹配一个字符,直到“*”的下一位能都匹配成功。
class Solution { public: bool isMatch(const char *s, const char *p) { const char *start = NULL, *tmp = NULL; while (*s != '\0') { if (*s == *p || *p == '?') { s++, p++; continue; } if (*p == '*') { start = p; p++; tmp = s; continue; } if (start != NULL) { p = start + 1; s = tmp + 1; tmp++; continue; } return false; } while (*p == '*') p++; return *p == '\0'; } };
相关文章推荐
- LeetCode: Wildcard Matching [043]
- ★leetcode44_Wildcard Matching
- LeetCode 44 Wildcard Matching(字符串匹配问题)
- leetcode--Wildcard Matching
- Leetcode题解---WildCard Matching Java实现
- Leetcode_Wildcard Matching
- 【leetcode】第44题 Wildcard Matching 题目+解析+代码
- Leetcode wild card matching
- [LeetCode]Wildcard Matching
- leetcode Wildcard Matching
- [Leetcode] Wildcard Matching (Java)
- LeetCode Wildcard Matching
- LeetCode Wildcard Matching
- LeetCode Wildcard Matching
- Leetcode 44. Wildcard Matching
- LeetCode Wildcard Matching DP/贪心
- 【leetcode】Wildcard Matching(hard) ★ 大神太牛了
- LeetCode 44.Wildcard Matching题目解析
- [LeetCode] Wildcard Matching
- LeetCode - Wildcard Matching