Skip to content

Commit c826de2

Browse files
committed
1106 added.
1 parent cdd8eda commit c826de2

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
project(D)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(D main.cpp)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/// Source : https://leetcode.com/problems/parsing-a-boolean-expression/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-06-30
4+
5+
#include <iostream>
6+
#include <cassert>
7+
#include <vector>
8+
9+
using namespace std;
10+
11+
12+
/// Recursion
13+
/// Time Complexity: O(n^2)
14+
/// Space Complexity: O(n)
15+
class Solution {
16+
public:
17+
bool parseBoolExpr(string exp) {
18+
19+
return parse(exp, 0, exp.size() - 1);
20+
}
21+
22+
private:
23+
bool parse(const string& exp, int l, int r){
24+
25+
if(l == r) return exp[l] == 't' ? true : false;
26+
27+
if(exp[l] == '!') return !parse(exp, l + 2, r - 1);
28+
29+
vector<int> comma = get_comma(exp, l + 2, r - 1);
30+
31+
if(comma.size() == 0) return parse(exp, l + 2, r - 1);
32+
33+
vector<bool> subs = {parse(exp, l + 2, comma[0] - 1)};
34+
for(int i = 0; i < comma.size(); i ++)
35+
subs.push_back(parse(exp, comma[i] + 1, i + 1 == comma.size() ? r - 1 : comma[i + 1] - 1));
36+
37+
bool res = subs[0];
38+
if(exp[l] == '&')
39+
for(int i = 1; i < subs.size(); i ++) res = res && subs[i];
40+
else
41+
for(int i = 1; i < subs.size(); i ++) res = res || subs[i];
42+
return res;
43+
}
44+
45+
vector<int> get_comma(const string& exp, int l, int r){
46+
47+
vector<int> res;
48+
int stack = 0;
49+
for(int i = l; i <= r; i ++)
50+
if(exp[i] == '(') stack ++;
51+
else if(exp[i] == ')') stack --;
52+
else if(exp[i] == ',' && !stack) res.push_back(i);
53+
return res;
54+
}
55+
};
56+
57+
58+
void print_bool(bool res){
59+
cout << (res ? "True" : "False") << endl;
60+
}
61+
62+
int main() {
63+
64+
string exp1 = "!(f)";
65+
print_bool(Solution().parseBoolExpr(exp1));
66+
// true
67+
68+
string exp2 = "|(f,t)";
69+
print_bool(Solution().parseBoolExpr(exp2));
70+
// true
71+
72+
string exp3 = "&(t,f)";
73+
print_bool(Solution().parseBoolExpr(exp3));
74+
// false
75+
76+
string exp4 = "|(&(t,f,t),!(t))";
77+
print_bool(Solution().parseBoolExpr(exp4));
78+
// false
79+
80+
string exp5 = "!(&(!(t),&(f),|(f)))";
81+
print_bool(Solution().parseBoolExpr(exp5));
82+
// true
83+
84+
85+
return 0;
86+
}

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,13 +746,14 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
746746
| 1093 | [Statistics from a Large Sample](https://leetcode.com/problems/statistics-from-a-large-sample/) | [] | [C++](1093-Statistics-from-a-Large-Sample/cpp-1093/) | | |
747747
| 1094 | [Car Pooling](https://leetcode.com/problems/car-pooling/) | [] | [C++](1094-Car-Pooling/cpp-1094/) | | |
748748
| 1095 | [Find in Mountain Array](https://leetcode.com/problems/find-in-mountain-array/) | [] | [C++](1095-Find-in-Mountain-Array/cpp-1095/) | | |
749-
| 1096 | [Brace Expansion II](https://leetcode.com/problems/brace-expansion-ii/) | [] | [C++](1096-Brace-Expansion-II/cpp-1096/) | | |
749+
| 1096 | [Brace Expansion II](https://leetcode.com/problems/brace-expansion-ii/) | []<br/>[缺:非递归算法] | [C++](1096-Brace-Expansion-II/cpp-1096/) | | |
750750
| | | | | | |
751751
| 1099 | [Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/) | [] | [C++](1099-Two-Sum-Less-Than-K/cpp-1099/) | | |
752752
| | | | | | |
753753
| 1103 | [Distribute Candies to People](https://leetcode.com/problems/distribute-candies-to-people/) | [solution](https://leetcode.com/problems/distribute-candies-to-people/solution/) | [C++](1103-Distribute-Candies-to-People/cpp-1103/) | | |
754754
| 1104 | [Path In Zigzag Labelled Binary Tree](https://leetcode.com/problems/path-in-zigzag-labelled-binary-tree/) | [] | [C++](1104-Path-In-Zigzag-Labelled-Binary-Tree/cpp-1104/) | | |
755755
| 1105 | [Filling Bookcase Shelves](https://leetcode.com/problems/filling-bookcase-shelves/) | [] | [C++](1105-Filling-Bookcase-Shelves/cpp-1105/) | | |
756+
| 1106 | [Parsing A Boolean Expression](https://leetcode.com/problems/parsing-a-boolean-expression/) | []<br/>[缺:非递归算法] | [C++](1106-Parsing-A-Boolean-Expression/cpp-1106/) | | |
756757
| | | | | | |
757758
| 1108 | [Defanging an IP Address](https://leetcode.com/problems/defanging-an-ip-address/) | [] | [C++](1108-Defanging-an-IP-Address/cpp-1108/) | | |
758759
| 1109 | [Corporate Flight Bookings](https://leetcode.com/problems/corporate-flight-bookings/) | [] | [C++](1109-Corporate-Flight-Bookings/cpp-1009/) | | |

0 commit comments

Comments
 (0)