Skip to content

Commit dd076d4

Browse files
committed
+ problem 1106
1 parent 911de8e commit dd076d4

File tree

5 files changed

+231
-0
lines changed

5 files changed

+231
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# 1106. Parsing A Boolean Expression
2+
A **boolean expression** is an expression that evaluates to either `true` or `false`. It can be in one of the following shapes:
3+
4+
* `'t'` that evaluates to `true`.
5+
* `'f'` that evaluates to `false`.
6+
* `'!(subExpr)'` that evaluates to **the logical NOT** of the inner expression `subExpr`.
7+
* `'&(subExpr1, subExpr2, ..., subExprn)'` that evaluates to **the logical AND** of the inner expressions `subExpr1, subExpr2, ..., subExprn` where `n >= 1`.
8+
* `'|(subExpr1, subExpr2, ..., subExprn)'` that evaluates to **the logical OR** of the inner expressions `subExpr1, subExpr2, ..., subExprn` where `n >= 1`.
9+
10+
Given a string `expression` that represents a **boolean expression**, return *the evaluation of that expression*.
11+
12+
It is **guaranteed** that the given expression is valid and follows the given rules.
13+
14+
#### Example 1:
15+
<pre>
16+
<strong>Input:</strong> expression = "&(|(f))"
17+
<strong>Output:</strong> false
18+
<strong>Explanation:</strong>
19+
First, evaluate |(f) --> f. The expression is now "&(f)".
20+
Then, evaluate &(f) --> f. The expression is now "f".
21+
Finally, return false.
22+
</pre>
23+
24+
#### Example 2:
25+
<pre>
26+
<strong>Input:</strong> expression = "|(f,f,f,t)"
27+
<strong>Output:</strong> true
28+
<strong>Explanation:</strong> The evaluation of (false OR false OR false OR true) is true.
29+
</pre>
30+
31+
#### Example 3:
32+
<pre>
33+
<strong>Input:</strong> expression = "!(&(f,t))"
34+
<strong>Output:</strong> true
35+
<strong>Explanation:</strong>
36+
First, evaluate &(f,t) --> (false AND true) --> false --> f. The expression is now "!(f)".
37+
Then, evaluate !(f) --> NOT false --> true. We return true.
38+
</pre>
39+
40+
#### Constraints:
41+
* <code>1 <= expression.length <= 2 * 10<sup>4</sup></code>
42+
* expression[i] is one following characters: `'('`, `')'`, `'&'`, `'|'`, `'!'`, `'t'`, `'f'`, and `','`.
43+
44+
## Solutions (Rust)
45+
46+
### 1. Solution
47+
```Rust
48+
impl Solution {
49+
pub fn parse_bool_expr(expression: String) -> bool {
50+
let mut ops = vec![];
51+
let mut bools = vec![];
52+
53+
for ch in expression.chars() {
54+
match ch {
55+
'!' | '&' | '|' => ops.push(ch),
56+
't' | 'f' => bools.push(Some(ch == 't')),
57+
')' => match ops.pop() {
58+
Some('!') => {
59+
let tmp = !bools.pop().unwrap().unwrap();
60+
bools.pop();
61+
bools.push(Some(tmp));
62+
}
63+
Some('&') => {
64+
let mut tmp = true;
65+
while let Some(Some(b)) = bools.pop() {
66+
tmp &= b;
67+
}
68+
bools.push(Some(tmp));
69+
}
70+
Some('|') => {
71+
let mut tmp = false;
72+
while let Some(Some(b)) = bools.pop() {
73+
tmp |= b;
74+
}
75+
bools.push(Some(tmp));
76+
}
77+
_ => {
78+
let tmp = bools.pop().unwrap();
79+
bools.pop();
80+
bools.push(tmp);
81+
}
82+
},
83+
'(' => bools.push(None),
84+
_ => (),
85+
}
86+
}
87+
88+
bools[0].unwrap()
89+
}
90+
}
91+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# 1106. 解析布尔表达式
2+
**布尔表达式** 是计算结果不是 `true` 就是 `false` 的表达式。有效的表达式需遵循以下约定:
3+
4+
* `'t'`,运算结果为 `true`
5+
* `'f'`,运算结果为 `false`
6+
* `'!(subExpr)'`,运算过程为对内部表达式 `subExpr` 进行 **逻辑非**(NOT)运算
7+
* `'&(subExpr1, subExpr2, ..., subExprn)'`,运算过程为对 2 个或以上内部表达式 `subExpr1, subExpr2, ..., subExprn` 进行 **逻辑与**(AND)运算
8+
* `'|(subExpr1, subExpr2, ..., subExprn)'`,运算过程为对 2 个或以上内部表达式 `subExpr1, subExpr2, ..., subExprn` 进行 **逻辑或**(OR)运算
9+
10+
给你一个以字符串形式表述的 [布尔表达式](https://baike.baidu.com/item/%E5%B8%83%E5%B0%94%E8%A1%A8%E8%BE%BE%E5%BC%8F/1574380?fr=aladdin) `expression`,返回该式的运算结果。
11+
12+
题目测试用例所给出的表达式均为有效的布尔表达式,遵循上述约定。
13+
14+
#### 示例 1:
15+
<pre>
16+
<strong>输入:</strong> expression = "&(|(f))"
17+
<strong>输出:</strong> false
18+
<strong>解释:</strong>
19+
首先,计算 |(f) --> f ,表达式变为 "&(f)" 。
20+
接着,计算 &(f) --> f ,表达式变为 "f" 。
21+
最后,返回 false 。
22+
</pre>
23+
24+
#### 示例 2:
25+
<pre>
26+
<strong>输入:</strong> expression = "|(f,f,f,t)"
27+
<strong>输出:</strong> true
28+
<strong>解释:</strong> 计算 (false OR false OR false OR true) ,结果为 true 。
29+
</pre>
30+
31+
#### 示例 3:
32+
<pre>
33+
<strong>输入:</strong> expression = "!(&(f,t))"
34+
<strong>输出:</strong> true
35+
<strong>解释:</strong>
36+
首先,计算 &(f,t) --> (false AND true) --> false --> f ,表达式变为 "!(f)" 。
37+
接着,计算 !(f) --> NOT false --> true ,返回 true 。
38+
</pre>
39+
40+
#### 提示:
41+
* <code>1 <= expression.length <= 2 * 10<sup>4</sup></code>
42+
* `expression[i]``'('``')'``'&'``'|'``'!'``'t'``'f'``','` 之一
43+
44+
## 题解 (Rust)
45+
46+
### 1. 题解
47+
```Rust
48+
impl Solution {
49+
pub fn parse_bool_expr(expression: String) -> bool {
50+
let mut ops = vec![];
51+
let mut bools = vec![];
52+
53+
for ch in expression.chars() {
54+
match ch {
55+
'!' | '&' | '|' => ops.push(ch),
56+
't' | 'f' => bools.push(Some(ch == 't')),
57+
')' => match ops.pop() {
58+
Some('!') => {
59+
let tmp = !bools.pop().unwrap().unwrap();
60+
bools.pop();
61+
bools.push(Some(tmp));
62+
}
63+
Some('&') => {
64+
let mut tmp = true;
65+
while let Some(Some(b)) = bools.pop() {
66+
tmp &= b;
67+
}
68+
bools.push(Some(tmp));
69+
}
70+
Some('|') => {
71+
let mut tmp = false;
72+
while let Some(Some(b)) = bools.pop() {
73+
tmp |= b;
74+
}
75+
bools.push(Some(tmp));
76+
}
77+
_ => {
78+
let tmp = bools.pop().unwrap();
79+
bools.pop();
80+
bools.push(tmp);
81+
}
82+
},
83+
'(' => bools.push(None),
84+
_ => (),
85+
}
86+
}
87+
88+
bools[0].unwrap()
89+
}
90+
}
91+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
impl Solution {
2+
pub fn parse_bool_expr(expression: String) -> bool {
3+
let mut ops = vec![];
4+
let mut bools = vec![];
5+
6+
for ch in expression.chars() {
7+
match ch {
8+
'!' | '&' | '|' => ops.push(ch),
9+
't' | 'f' => bools.push(Some(ch == 't')),
10+
')' => match ops.pop() {
11+
Some('!') => {
12+
let tmp = !bools.pop().unwrap().unwrap();
13+
bools.pop();
14+
bools.push(Some(tmp));
15+
}
16+
Some('&') => {
17+
let mut tmp = true;
18+
while let Some(Some(b)) = bools.pop() {
19+
tmp &= b;
20+
}
21+
bools.push(Some(tmp));
22+
}
23+
Some('|') => {
24+
let mut tmp = false;
25+
while let Some(Some(b)) = bools.pop() {
26+
tmp |= b;
27+
}
28+
bools.push(Some(tmp));
29+
}
30+
_ => {
31+
let tmp = bools.pop().unwrap();
32+
bools.pop();
33+
bools.push(tmp);
34+
}
35+
},
36+
'(' => bools.push(None),
37+
_ => (),
38+
}
39+
}
40+
41+
bools[0].unwrap()
42+
}
43+
}

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@
670670
[1094][1094l]|[Car Pooling][1094] |![rs]
671671
[1103][1103l]|[Distribute Candies to People][1103] |![py]
672672
[1104][1104l]|[Path In Zigzag Labelled Binary Tree][1104] |![rs]
673+
[1106][1106l]|[Parsing A Boolean Expression][1106] |![rs]
673674
[1108][1108l]|[Defanging an IP Address][1108] |![rs]
674675
[1109][1109l]|[Corporate Flight Bookings][1109] |![rs]
675676
[1110][1110l]|[Delete Nodes And Return Forest][1110] |![py]&nbsp;&nbsp;![rb]
@@ -2069,6 +2070,7 @@
20692070
[1094]:Problemset/1094-Car%20Pooling/README.md#1094-car-pooling
20702071
[1103]:Problemset/1103-Distribute%20Candies%20to%20People/README.md#1103-distribute-candies-to-people
20712072
[1104]:Problemset/1104-Path%20In%20Zigzag%20Labelled%20Binary%20Tree/README.md#1104-path-in-zigzag-labelled-binary-tree
2073+
[1106]:Problemset/1106-Parsing%20A%20Boolean%20Expression/README.md#1106-parsing-a-boolean-expression
20722074
[1108]:Problemset/1108-Defanging%20an%20IP%20Address/README.md#1108-defanging-an-ip-address
20732075
[1109]:Problemset/1109-Corporate%20Flight%20Bookings/README.md#1109-corporate-flight-bookings
20742076
[1110]:Problemset/1110-Delete%20Nodes%20And%20Return%20Forest/README.md#1110-delete-nodes-and-return-forest
@@ -3467,6 +3469,7 @@
34673469
[1094l]:https://leetcode.com/problems/car-pooling/
34683470
[1103l]:https://leetcode.com/problems/distribute-candies-to-people/
34693471
[1104l]:https://leetcode.com/problems/path-in-zigzag-labelled-binary-tree/
3472+
[1106l]:https://leetcode.com/problems/parsing-a-boolean-expression/
34703473
[1108l]:https://leetcode.com/problems/defanging-an-ip-address/
34713474
[1109l]:https://leetcode.com/problems/corporate-flight-bookings/
34723475
[1110l]:https://leetcode.com/problems/delete-nodes-and-return-forest/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@
670670
[1094][1094l]|[拼车][1094] |![rs]
671671
[1103][1103l]|[分糖果 II][1103] |![py]
672672
[1104][1104l]|[二叉树寻路][1104] |![rs]
673+
[1106][1106l]|[解析布尔表达式][1106] |![rs]
673674
[1108][1108l]|[IP 地址无效化][1108] |![rs]
674675
[1109][1109l]|[航班预订统计][1109] |![rs]
675676
[1110][1110l]|[删点成林][1110] |![py]&nbsp;&nbsp;![rb]
@@ -2069,6 +2070,7 @@
20692070
[1094]:Problemset/1094-Car%20Pooling/README_CN.md#1094-拼车
20702071
[1103]:Problemset/1103-Distribute%20Candies%20to%20People/README_CN.md#1103-分糖果-ii
20712072
[1104]:Problemset/1104-Path%20In%20Zigzag%20Labelled%20Binary%20Tree/README_CN.md#1104-二叉树寻路
2073+
[1106]:Problemset/1106-Parsing%20A%20Boolean%20Expression/README_CN.md#1106-解析布尔表达式
20722074
[1108]:Problemset/1108-Defanging%20an%20IP%20Address/README_CN.md#1108-ip-地址无效化
20732075
[1109]:Problemset/1109-Corporate%20Flight%20Bookings/README_CN.md#1109-航班预订统计
20742076
[1110]:Problemset/1110-Delete%20Nodes%20And%20Return%20Forest/README_CN.md#1110-删点成林
@@ -3467,6 +3469,7 @@
34673469
[1094l]:https://leetcode.cn/problems/car-pooling/
34683470
[1103l]:https://leetcode.cn/problems/distribute-candies-to-people/
34693471
[1104l]:https://leetcode.cn/problems/path-in-zigzag-labelled-binary-tree/
3472+
[1106l]:https://leetcode.cn/problems/parsing-a-boolean-expression/
34703473
[1108l]:https://leetcode.cn/problems/defanging-an-ip-address/
34713474
[1109l]:https://leetcode.cn/problems/corporate-flight-bookings/
34723475
[1110l]:https://leetcode.cn/problems/delete-nodes-and-return-forest/

0 commit comments

Comments
 (0)