[LeetCode] Regular Expression Matching

http://oj.leetcode.com/problems/regular-expression-matching/

这就是 小龙说的简单题, 真沙茶做了一晚上, 试了又试, 各种简化, 我已经快疯掉了, 结果超时了,

网上查了下这道题居然有dp解法, 顺带发现了我代码里面的错误, 改了回来 AC了.  我是真沙茶.

如果下个字符不是*好办, 如果是*就dfs.

 1 class Solution {
 2 public:
 3     bool isMatch(const char *s, const char *p) {
 4         // IMPORTANT: Please reset any member data you declared, as
 5         // the same Solution instance will be reused for each test case.    
 6         if (*p == 0 && *s == 0) {
 7             return true;
 8         } else if (*p == 0 && *s != 0) {
 9             return false;
10         } 
11         if (*(p + 1) == '*') {
12             while((*s == *p) || (*s != 0 && *p == '.')) {
13                 if (isMatch(s++, p + 2)) return true;
14             }
15             return isMatch(s, p + 2);
16         } else if ((*s != 0 && *p == '.') || *p == *s) {
17             return isMatch(s + 1, p + 1);
18         } else {
19             return false;
20         }
21     }
22 };

 

posted @ 2013-11-09 02:33  NextLife  阅读(208)  评论(0)    收藏  举报