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) {
           const char * ptr;
           const char *str;
           bool start = false;
           for(ptr=p,str=s; *str!=''  ; ){
               switch(*ptr){
                   case '?':
                   str++,ptr++;
                   break;
                   case '*':
                   start = true;
                   while(*ptr == '*')
                     ptr++;
                   if(*ptr=='')
                   return true;
                   p = ptr;
                   s = str;
                   break;
                   default:
                   if(*ptr != *str){
                       if(!start)
                       return false;
                       ptr = p;
                       str = s+1;
                       s=s+1;
                   }
                  else{
                      ptr++;
                      str++;
                  }
               }
           }
           while(*ptr=='*')  ptr++;
           
           return (*ptr == '');
        }
    };


  • 相关阅读:
    python 迭代器
    python 装饰器
    python 函数进阶
    python 函数
    python文件操作
    python 集合 深浅拷贝
    python基础之循环
    python基础之字典
    python基础之操作列表
    python基础之列表
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6845710.html
Copyright © 2011-2022 走看看