leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1106.cpp (1226B)
0 class Solution {
1 public:
2 bool parseBoolExpr(const string &expression) const {
3 bool state = false;
4 stack<char> op;
6 const auto flush = [&](int &i) {
7 for (int count = 0; i < size(expression); i++) {
8 if (expression[i] == '(')
9 count++;
10 else if (expression[i] == ')' && count-- == 0)
11 break;
12 }
13 op.pop();
14 };
16 for (int i = 0; i < size(expression); i++) {
17 switch (expression[i]) {
18 case 't': state = true; break;
19 case 'f': state = false; break;
20 case '!':
21 case '&':
22 case '|': op.push(expression[i]); break;
23 case ',':
24 // lazy evaluation
25 switch (op.top()) {
26 case '&':
27 if (!state) flush(i);
28 break;
29 case '|':
30 if (state) flush(i);
31 break;
32 }
33 break;
34 case ')':
35 if (op.top() == '!') state = !state;
37 op.pop();
38 break;
39 }
40 }
42 return state;
43 }
44 };