• [LeetCode]Wildcard Matching


    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) {
            bool star = false;
            const char* starPtr = NULL;
            const char* savePtr = NULL;
            while(*s != '')
            {
                if(*p == '?') s++, p++;
                else if(*p == '*')
                {
                    while(*p == '*') p++;
                    p--;
                    starPtr = p;
                    p++;
                    savePtr = s;
                    star = true;
                }
                else 
                {
                    if(*s == *p) s++, p++;
                    else
                    {
                        if(star == true) s = ++savePtr, p = starPtr+1;//depend on star
                        else return false;
                    }
                }
            }
            while(*p == '*') p++;
            return (*s == '' && *p == '');
        }
    };
    

      

  • 相关阅读:
    第十周学习进度条
    第九周学习进度条
    Runner站立会议08
    Runner站立会议07
    构建之法阅读笔记02
    Runner站立会议04
    学习进度条
    Runner站立会议01
    进度条
    返回一个一维整数数组中最大子数组的和
  • 原文地址:https://www.cnblogs.com/Rosanna/p/3481849.html
Copyright © 2020-2023  润新知