-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsolution010.cpp
More file actions
48 lines (41 loc) · 934 Bytes
/
solution010.cpp
File metadata and controls
48 lines (41 loc) · 934 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* @file Regular Expression Matching
* cpselvis([email protected])
* 2016.7.31
*/
#include<cstdio>
class Solution {
public:
bool isMatch(const char *s, const char *p) {
if (s == NULL || p == NULL)
return false;
if (*p == '\0')
return *s == '\0';
if (*(p + 1) == '*')
{
while ((*s != '\0' && *p == '.') || *s == *p)
{
if (isMatch(s, p + 2))
return true;
s ++;
}
return isMatch(s, p + 2);
}
else if ((*s != '\0' && *p == '.') || *s == *p)
{
return isMatch(s + 1, p + 1);
}
return false;
}
};
int main(int argc, char **argv)
{
Solution s;
printf("%d\n", s.isMatch("aa", "a"));
printf("%d\n", s.isMatch("aa", "aa"));
printf("%d\n", s.isMatch("aaa", "aa"));
printf("%d\n", s.isMatch("aa", "a*"));
printf("%d\n", s.isMatch("aa", ".*"));
printf("%d\n", s.isMatch("ab", ".*"));
printf("%d\n", s.isMatch("aab", "c*a*b"));
}