zoukankan      html  css  js  c++  java
  • Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'.

    '.' Matches any single character.
    '*' Matches zero or more of the preceding element.
    
    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", "a*") → true
    isMatch("aa", ".*") → true
    isMatch("ab", ".*") → true
    isMatch("aab", "c*a*b") → true

    思路:

    代码:

     1     bool isMatch(const char *s, const char *p) {
     2         // IMPORTANT: Please reset any member data you declared, as
     3         // the same Solution instance will be reused for each test case.  
     4         if(*p == '')
     5             return *s == '';
     6         if(*(p+1) == '*'){
     7             for(const char *t = s; *t != '' && (*t == *p || *p == '.'); t++){
     8                 if(isMatch(t+1, p+2))
     9                     return true;
    10             }
    11             return isMatch(s, p+2);
    12         }
    13         else{
    14             if(*s != '' && (*s == *p || *p == '.'))
    15                 return isMatch(s+1, p+1);
    16             return false;
    17         }
    18     }
  • 相关阅读:
    Ghost博客安装
    PHP变量作用域
    ssh文件传输命令:sz与rz命令
    excel怎么固定第一行
    memcache和redis区别
    Memcache分布式部署方案
    Memcache服务器端参数说明
    Memcache基础教程
    在Windows下安装Memcached
    MySQL体系结构和存储引擎概述
  • 原文地址:https://www.cnblogs.com/waruzhi/p/3441863.html
Copyright © 2011-2022 走看看