leetcode 044 —— Wildcard Matching

来源:互联网 发布:数据库与数据挖掘 编辑:程序博客网 时间:2024/04/29 16:52

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") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "*") → trueisMatch("aa", "a*") → trueisMatch("ab", "?*") → trueisMatch("aab", "c*a*b") → false

思路:

class Solution {public:bool isMatch(string s, string p) {int i = 0, j = 0;int star=-1;int ss=0;while (s[i]){if ((s[i] == p[j]) || (p[j] == '?')){i++;j++;continue;}if (p[j] == '*'){star = j++;ss = i;continue;}if (star>=0){j = star + 1;i = ++ss;continue;}return false;}while (p[j] == '*') j++;if (p[j])return false;    //如果p[j]是null,那么!p[j]就是yes,如果是elsereturn true;}};

0 0