Leetcode_wildcard-matching(c++ and python version)
2014-04-10 22:17
477 查看
地址:http://oj.leetcode.com/problems/wildcard-matching/
Implement wildcard pattern matching with support for
思路:(1)如果*s与*p相等或者*p=='?', s++, p++, 继续下一次判断;
(2)若*p=='*', 先用copyp和copys分别把p和s存一份(其实存的是起始位置,可以做*匹配的起始位置), s的位置不变,++p (这里其实是*不匹配任何,若之后不匹配,再退回来匹配)
(3)若copyp不为空,即之前存在*,现在可以匹配,p = copyp + 1, 即从*之后开始, s = ++copys, s也是从当初copys开始,不过copys本身也要自增(记录*已匹配字符数),每自增一次,代表p中的*匹配多一个字符,每次如此p = copyp + 1, 从*后开始匹配,不匹配的话再重复(即*再多匹配s中的一个字符), 直到s为空.
(4)前三都不符合,无法匹配。
可参考:http://blog.csdn.net/doc_sgl/article/details/12721187
c++参考代码:
python 参考代码:
python的代码真是折腾死了,没有指针真是不方便啊!
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
思路:(1)如果*s与*p相等或者*p=='?', s++, p++, 继续下一次判断;
(2)若*p=='*', 先用copyp和copys分别把p和s存一份(其实存的是起始位置,可以做*匹配的起始位置), s的位置不变,++p (这里其实是*不匹配任何,若之后不匹配,再退回来匹配)
(3)若copyp不为空,即之前存在*,现在可以匹配,p = copyp + 1, 即从*之后开始, s = ++copys, s也是从当初copys开始,不过copys本身也要自增(记录*已匹配字符数),每自增一次,代表p中的*匹配多一个字符,每次如此p = copyp + 1, 从*后开始匹配,不匹配的话再重复(即*再多匹配s中的一个字符), 直到s为空.
(4)前三都不符合,无法匹配。
可参考:http://blog.csdn.net/doc_sgl/article/details/12721187
c++参考代码:
class Solution { public: bool isMatch(const char *s, const char *p) { const char *copyp = NULL, *copys = s; while(*s) { if(*s==*p || *p=='?') { ++s; ++p; } else if(*p=='*') { copyp = p++; copys = s; } else if(copyp) { p = copyp+1; s = ++copys; } else return false; } while(*p && *p=='*') ++p; return !(*p); } };
python 参考代码:
class Solution: # @param s, an input string # @param p, a pattern string # @return a boolean def isMatch(self, s, p): p += '\0' copys = copyp = "" flag = True while s: if s[0] == p[0] or p[0] == '?': s = s[1:] p = p[1:] elif p[0] == '*': copyp = p copys = s p = p[1:] elif copyp: copys = copys[1:] s = copys p = copyp[1:] else: return False while p and p[0]=='*': p = p[1:] return p[0]=='\0'
python的代码真是折腾死了,没有指针真是不方便啊!
相关文章推荐
- Leetcode_longest-palindromic-substring(c++ and python version)
- Leetcode_populating-next-right-pointers-in-each-node(updated c++ and python version)
- Leetcode_minimum-depth-of-binary-tree (c++ and python version)
- Leetcode_climbing-statirs (c++ and python version)
- **Leetcode_copy-list-with-random-pointer (c++ and python version)
- Leetcode_search-in-rotated-sorted-array-ii(updated c++ and python version)
- Leetcode_maximum-depth-of-binary-tree (updated c++ and python version)
- **Leetcode_container-with-most-water(c++ and python version)
- Leetcode_powx-n(c++ and python version)
- Leetcode_count-and-say(c++ and python version)
- Leetcode_jump-game (updated c++ and python version)
- Leetcode_remove-nth-node-from-end-of-list(c++ and python version)
- Leetcode_remove-duplicates-from-sorted-list (updated c++ and python version)
- Leetcode_two-sum (c++ and python version)
- Leetcode_valid-number(updated c++ and python version)
- Leetcode_trapping-rain-water(updated c++ and python version)
- Leetcode_path-sum (updated c++ and python version)
- Leetcode_first-missing-positive (updated c++ and python version)
- Leetcode_best-time-to-buy-and-sell-stock-ii(c++ and python version)
- Leetcode_word-break(c++ and python version)