0%

leetcode.44.通配符匹配(动态规划)

正则表达式

题目

- 44. 通配符匹配

递归式动态规划

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
49
50
51
52
53
54
55
56
57
class Solution {
public:
string ss;
string pp;
map<int, bool>* m=new std::map<int, bool>();
bool isMatchRecursive( int i, int j)
{
int key = i * (ss.length() + 10) * (pp.length() + 10)+ j;
if (m->find(key) != m->end())
{
return m->at(key);
}
bool value = false;
if (j < 0)
{
if (i < 0)
{
value = true;
}
else
{
value = false;
}
}
else if (i < 0)
{
if (pp[j] == '*')
{
value = isMatchRecursive( i, j - 1);
}
else
{
value = false;
}
}
else if (pp[j] == '*')
{
value = isMatchRecursive(i,j - 1) || isMatchRecursive(i - 1,j);
}
else if (ss[i] == pp[j] || pp[j] == '?')
{
value = isMatchRecursive( i - 1, j - 1);
}
else
{
value = false;
}
m->insert({ key,value });
return value;
}
bool isMatch(string s, string p) {
ss = s;
pp = p;
bool result = isMatchRecursive( ss.length() - 1, pp.length() - 1);
return result;
}
};