zoukankan      html  css  js  c++  java
  • [剑指Offer] 52.正则表达式匹配

    题目描述

    请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配

     1 class Solution
     2 {
     3 public:
     4     bool match(char* str,char* pattern)
     5     {
     6         //当存在空指针时,返回false
     7         if(str == NULL || pattern == NULL) return false;
     8         //如果两字符串都到结尾,返回true
     9         if(*str == '' && *pattern == '')
    10             return true;
    11         //如果模式串的第二个字符是"*"
    12         if(*(pattern + 1) == '*')
    13             //如果首字符匹配
    14             if(*str == *pattern || (*pattern == '.' && *str != ''))
    15                 return match(str,pattern+2)||match(str+1,pattern+2)||match(str+1,pattern);
    16             else//首字符不匹配
    17                 return match(str,pattern+2);
    18         //如果模式串第二个字符不是"*",且首字符匹配,两字符串同时后移
    19         if(*str == *pattern || (*pattern == '.' && *str != ''))
    20             return match(str+1,pattern+1);
    21         return false;
    22     }
    23 };
  • 相关阅读:
    Docker
    Alfred Workflow
    AWS Lambda
    XPath
    WebMagic
    Splash
    Selenium
    代理服务器
    JSONPath
    Sqlserver 查询分组 记录
  • 原文地址:https://www.cnblogs.com/lca1826/p/6549414.html
Copyright © 2011-2022 走看看