-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1106-parsing-a-boolean-expression.cpp
37 lines (37 loc) · 1.21 KB
/
1106-parsing-a-boolean-expression.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
class Solution {
public:
bool parseBoolExpr(string expression) {
vector<char> ex, val;
for (auto& i : expression) {
if (i == ',') continue;
if (i == '!' || i == '&' || i == '|') {
ex.push_back(i);
} else if (i != ')') {
val.push_back(i);
} else {
int op = ex.back();
ex.pop_back();
char c = val.back();
if (op == '!') {
val.pop_back();
val.pop_back();
c = (c == 't' ? 'f' : 't');
} else if (op == '&') {
while (val.back() != '(') {
c = ((c == 'f' || val.back() == 'f') ? 'f' : 't');
val.pop_back();
}
val.pop_back();
} else {
while (val.back() != '(') {
c = ((c == 't' || val.back() == 't') ? 't' : 'f');
val.pop_back();
}
val.pop_back();
}
val.push_back(c);
}
}
return val.back() == 'f' ? 0 : 1;
}
};