zoukankan      html  css  js  c++  java
  • 正则表达式匹配

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

    leetcode原题

    Regular Expression Matching

    思路:只有当p==时,判断s==;否则会出现:s=,p=c*。

     先判断是否p+1==*,然后进入循环,循环结束return match(str,p+2),不然会出现s=,p=.*,进入不了循环的状况。

    else if 时要加上str!=,不然会出现s=,p=.的状况,都+1后s=null。

     1 class Solution {
     2 public:
     3     bool match(char* str, char* pattern)
     4     {
     5         if(*pattern=='')
     6             return *str=='';
     7         if(*(pattern+1)=='*'){
     8             while(*str!=''){
     9                 if(match(str,pattern+2))
    10                     return true;
    11                 if(*str==*pattern||*pattern=='.')
    12                     str++;
    13                 else
    14                     return false;
    15             }
    16             return match(str,pattern+2);
    17         }
    18         else if(*pattern==*str||(*pattern=='.'&&*str!='')){
    19             return match(str+1,pattern+1);
    20         }
    21         return false;
    22     }
    23 };
  • 相关阅读:
    【bzoj1010】[HNOI2008]玩具装箱toy
    bzoj 3173
    bzoj 1179
    bzoj 2427
    bzoj 1051
    bzoj 1877
    bzoj 1066
    bzoj 2127
    bzoj 1412
    bzoj 3438
  • 原文地址:https://www.cnblogs.com/zl1991/p/4795723.html
Copyright © 2011-2022 走看看