zoukankan      html  css  js  c++  java
  • 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

    思路:

    先是递归,不能过大集合;

    循环可以过,但是p == '*'的情况需要特殊处理,用两个指针记录p == '*'时的s和p的位置。

    因为p的*可以代表很多字符,需要确定*代表s中的那些字符

    代码:

    递归版

    class Solution {
    public:
        bool isMatch(const char *s, const char *p) {
            if (*p == '*')
            {
                while (*p == '*')   p++;
                if (*p == '') return true;
                while (*s != '' && !isMatch(s, p))
                    s++;
                return *s != '';
            }
            else if (*p == '' || *s == '')
                return *p == *s;
            else if (*p == '?' || *p == *s)
                return isMatch(++s, ++p);
            else return false;
        }
    };
    

     循环版

    class Solution {
    public:
        bool isMatch(const char *s, const char *p)
        {
            if (!s && !p)
                return true;
    
            const char *ss = NULL;
            const char *sp = NULL;
    
            while (*s)
            {
                if (*s == *p || *p == '?')
                {
                    s++;
                    p++;
                }
                else if (*p == '*')
                {
                    while (*p == '*')
                        p++;
                    if (*p == '')
                        return true;
                    ss = s;
                    sp = p;
                }
                else if ((*p == '' || *p != *s) && sp)
                {
                    s = ++ss;
                    p = sp;
                }
                else return false;
            }
            while (*p)
                if (*p++ != '*')
                    return false;
            return true;
        }
    };
  • 相关阅读:
    第10天 面向对象
    ubuntu16.04安装openssh中报错解决
    白帽子讲web安全——访问控制
    白帽子讲web安全——认证与会话管理
    常见的文件上传绕过和文件解析漏洞
    常见的文件包含漏洞
    红队在Windows 10上迁徙问题
    Mimikatz 法国神器
    端口转发 Port Forwarding (一)
    SOAR平台初探(一)
  • 原文地址:https://www.cnblogs.com/dollarzhaole/p/3250983.html
Copyright © 2011-2022 走看看