forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1106.cpp
60 lines (56 loc) · 1.07 KB
/
1106.cpp
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
58
59
60
class Solution {
public:
bool parseBoolExpr(string expression) {
this->s = expression;
int i = 0;
return parseStr(i);
}
string s;
bool parseStr(int& i)
{
if (s[i] == 't')
{
++i;
return true;
}
else if (s[i] == 'f')
{
++i;
return false;
}
else if (s[i] == '&') return parseAnd(i);
else if (s[i] == '|') return parseOr(i);
else return parseNot(i);
}
bool parseAnd(int& i)
{
i += 2;
bool res = parseStr(i);
while (s[i] != ')')
{
i++;
res &= parseStr(i);
}
i++;
return res;
}
bool parseOr(int& i)
{
i += 2;
bool res = parseStr(i);
while (s[i] != ')')
{
i++;
res |= parseStr(i);
}
i++;
return res;
}
bool parseNot(int& i)
{
i += 2;
bool res = !parseStr(i);
i++;
return res;
}
};