leetcode Regular Expression Matching

 现在回过头来看这个代码,由于最近编译原理实验课上写了词法分析程序,这两个还是有相似的地方的,词法分析程序我是一层大循环控制eof(),分类的小循环去找标识符,无符号数,分割符等。其实和这个题的思想差不多。

这个题是用递归往下推进,中间匹配*号时用while去尝试,失败再退回,成功则继续。

代码:

 1 class Solution {
 2 public:
 3     bool isMatch(const char *s, const char *p) {
 4         if (*(p) == '')
 5         return *(s) == 0;
 6     if (*(p + 1) != '*')
 7     {
 8         if (*(s) == *(p) || *(p) == '.'&&*(s) != '')
 9         {
10             return isMatch(s+1, p+1);
11         }
12         else
13             return false;
14     }
15     else
16     {
17         while (*(s) == *(p) || *(p) == '.'&&*(s) != '')
18         {
19             if (isMatch(s, p + 2))
20             {
21                 return true;
22             }
23             s++;
24         }
25         return  isMatch(s, p + 2);
26     }
27     }
28 };
原文地址:https://www.cnblogs.com/chaiwentao/p/4370275.html