zoukankan      html  css  js  c++  java
  • [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 == '');
        }
    };
    

      

  • 相关阅读:
    随笔练习
    获得屏幕相关的辅助类
    C# 下sqlite简单使用
    XP系统下 VS2010 选中行崩溃
    Custome Buble Data Point
    RIA
    Chart Style
    d3js
    TreeView
    [Java入门笔记] Java语言简介
  • 原文地址:https://www.cnblogs.com/Rosanna/p/3481849.html
Copyright © 2011-2022 走看看