首页 > 其他 > 详细

Leetcode 之Regular Expression Matching(31)

时间:2016-05-24 16:49:30      阅读:223      评论:0      收藏:0      [点我收藏+]

技术分享

正则表达式的匹配,还是挺难的。可根据下一个字符是不是*分为两种情况处理,需要考虑多种情况。

技术分享
bool isMatch(const char *s, const char *p)
      {
          if (*p == \0)return *s == \0;

          //如果下一个不是*(*可表示前一个字符的数量)
          //要么当前字符匹配,要么是.,不可跳过
          if (*(p + 1) != *)
          {
              if (*s == *p || (*p == . && *s != \0))
                  return isMatch(s + 1, p + 1);
              else
                  return false;
          }
          else
          {
              //如果是*,则当前字符匹配|| 有一个为.
              //因为后面是*,即使不完全匹配也没关系,跳过即可
              while (*p == *s || (*p == . && *s != \0))
              {
                  if (isMatch(s, p + 2))
                      return true;
                  s++;
              }
              return isMatch(s, p + 2);
          }
      }
View Code

 

Leetcode 之Regular Expression Matching(31)

原文:http://www.cnblogs.com/573177885qq/p/5523819.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!