zoukankan      html  css  js  c++  java
  • 【leetcode】Regular Expression Matching

    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 class Solution {
     2 public:
     3     bool isMatch(const char *s, const char *p) {
     4        
     5         if(s==NULL||p==NULL) return false;
     6         if(*p==''&&*s=='') return true;
     7         if(*p==''&&*s!='') return false;
     8      
     9         //如果模式串下一个字符为*    
    10         if(*(p+1)=='*')
    11         {
    12             //循环比较当前字符与模式串字符是否相等
    13             while((*s!=''&&*p=='.')||(*s==*p))
    14             {
    15                 //防止这种情况出现:"aaa", "a*a"
    16                 if(isMatch(s,p+2)) return true;
    17                 s++;
    18             }
    19             return isMatch(s,p+2);
    20         }
    21         else if((*s!=''&&*p=='.')||*s==*p)
    22         {
    23             //如果当前元素相等,则开始匹配下一个元素
    24             return isMatch(s+1,p+1);
    25         }
    26         return false;
    27     }
    28 };
     
     
  • 相关阅读:
    xml实现AOP
    AOP
    python 大小写转换方法(全)
    vs2013 快捷键
    构造方法后面带:this()
    资源的使用----实例分析
    把列表变成用指定字符连接的字符串
    python 列表、元组操作
    工作踩坑记录:JavaScript跳转被缓存
    《Effective Java》读书笔记
  • 原文地址:https://www.cnblogs.com/reachteam/p/4209445.html
Copyright © 2011-2022 走看看