Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 3fc52c9

Browse files
committed
1106 added
1 parent 7f2ee71 commit 3fc52c9

File tree

4 files changed

+135
-32
lines changed

4 files changed

+135
-32
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# [1106. Parsing A Boolean Expression](https://leetcode.com/problems/parsing-a-boolean-expression/)
2+
3+
Return the result of evaluating a given boolean expression, represented as a string.
4+
5+
An expression can either be:
6+
7+
- "t", evaluating to True;
8+
- "f", evaluating to False;
9+
- "!(expr)", evaluating to the logical NOT of the inner expression expr;
10+
- "&(expr1,expr2,...)", evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...;
11+
- "|(expr1,expr2,...)", evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...
12+
13+
Example 1:
14+
15+
```text
16+
Input: expression = "!(f)"
17+
Output: true
18+
```
19+
20+
Example 2:
21+
22+
```text
23+
Input: expression = "|(f,t)"
24+
Output: true
25+
```
26+
27+
Example 3:
28+
29+
```text
30+
Input: expression = "&(t,f)"
31+
Output: false
32+
```
33+
34+
Example 4:
35+
36+
```text
37+
Input: expression = "|(&(t,f,t),!(t))"
38+
Output: false
39+
```
40+
41+
Constraints:
42+
43+
- 1 <= expression.length <= 20000
44+
- expression[i] consists of characters in {'(', ')', '&', '|', '!', 't', 'f', ','}.
45+
- expression is a valid expression representing a boolean, as given in the description.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package problem1106
2+
3+
func parseBoolExpr(expression string) bool {
4+
5+
return false
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package problem1106
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// tcs is testcase slice
10+
var tcs = []struct {
11+
expression string
12+
ans bool
13+
}{
14+
15+
{
16+
"!(f)",
17+
true,
18+
},
19+
20+
{
21+
"|(f,t)",
22+
true,
23+
},
24+
25+
{
26+
"&(t,f)",
27+
false,
28+
},
29+
30+
{
31+
"|(&(t,f,t),!(t))",
32+
false,
33+
},
34+
35+
// 可以有多个 testcase
36+
}
37+
38+
func Test_parseBoolExpr(t *testing.T) {
39+
ast := assert.New(t)
40+
41+
for _, tc := range tcs {
42+
ast.Equal(tc.ans, parseBoolExpr(tc.expression), "输入:%v", tc)
43+
}
44+
}
45+
46+
func Benchmark_parseBoolExpr(b *testing.B) {
47+
for i := 0; i < b.N; i++ {
48+
for _, tc := range tcs {
49+
parseBoolExpr(tc.expression)
50+
}
51+
}
52+
}

leetcode.json

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"Username": "aQuaYi",
33
"Ranking": 555,
4-
"Updated": "2019-08-02T20:08:30.699402383+08:00",
4+
"Updated": "2019-08-03T08:38:50.524716233+08:00",
55
"Record": {
66
"Easy": {
7-
"Solved": 253,
8-
"Total": 255
7+
"Solved": 254,
8+
"Total": 256
99
},
1010
"Medium": {
11-
"Solved": 435,
12-
"Total": 442
11+
"Solved": 434,
12+
"Total": 441
1313
},
1414
"Hard": {
1515
"Solved": 180,
@@ -529,7 +529,7 @@
529529
"ID": 42,
530530
"Title": "Trapping Rain Water",
531531
"TitleSlug": "trapping-rain-water",
532-
"PassRate": "43%",
532+
"PassRate": "44%",
533533
"Difficulty": "Hard",
534534
"IsAccepted": true,
535535
"IsPaid": false,
@@ -2737,7 +2737,7 @@
27372737
"ID": 226,
27382738
"Title": "Invert Binary Tree",
27392739
"TitleSlug": "invert-binary-tree",
2740-
"PassRate": "58%",
2740+
"PassRate": "59%",
27412741
"Difficulty": "Easy",
27422742
"IsAccepted": true,
27432743
"IsPaid": false,
@@ -3085,7 +3085,7 @@
30853085
"ID": 255,
30863086
"Title": "Verify Preorder Sequence in Binary Search Tree",
30873087
"TitleSlug": "verify-preorder-sequence-in-binary-search-tree",
3088-
"PassRate": "43%",
3088+
"PassRate": "44%",
30893089
"Difficulty": "Medium",
30903090
"IsAccepted": false,
30913091
"IsPaid": true,
@@ -3614,7 +3614,7 @@
36143614
"Title": "Bulls and Cows",
36153615
"TitleSlug": "bulls-and-cows",
36163616
"PassRate": "40%",
3617-
"Difficulty": "Medium",
3617+
"Difficulty": "Easy",
36183618
"IsAccepted": true,
36193619
"IsPaid": false,
36203620
"IsFavor": false,
@@ -8161,7 +8161,7 @@
81618161
"ID": 678,
81628162
"Title": "Valid Parenthesis String",
81638163
"TitleSlug": "valid-parenthesis-string",
8164-
"PassRate": "32%",
8164+
"PassRate": "33%",
81658165
"Difficulty": "Medium",
81668166
"IsAccepted": true,
81678167
"IsPaid": false,
@@ -9445,7 +9445,7 @@
94459445
"ID": 785,
94469446
"Title": "Is Graph Bipartite?",
94479447
"TitleSlug": "is-graph-bipartite",
9448-
"PassRate": "43%",
9448+
"PassRate": "44%",
94499449
"Difficulty": "Medium",
94509450
"IsAccepted": true,
94519451
"IsPaid": false,
@@ -9529,7 +9529,7 @@
95299529
"ID": 792,
95309530
"Title": "Number of Matching Subsequences",
95319531
"TitleSlug": "number-of-matching-subsequences",
9532-
"PassRate": "43%",
9532+
"PassRate": "44%",
95339533
"Difficulty": "Medium",
95349534
"IsAccepted": true,
95359535
"IsPaid": false,
@@ -10249,7 +10249,7 @@
1024910249
"ID": 852,
1025010250
"Title": "Peak Index in a Mountain Array",
1025110251
"TitleSlug": "peak-index-in-a-mountain-array",
10252-
"PassRate": "69%",
10252+
"PassRate": "70%",
1025310253
"Difficulty": "Easy",
1025410254
"IsAccepted": true,
1025510255
"IsPaid": false,
@@ -11329,7 +11329,7 @@
1132911329
"ID": 942,
1133011330
"Title": "DI String Match",
1133111331
"TitleSlug": "di-string-match",
11332-
"PassRate": "70%",
11332+
"PassRate": "69%",
1133311333
"Difficulty": "Easy",
1133411334
"IsAccepted": true,
1133511335
"IsPaid": false,
@@ -11377,7 +11377,7 @@
1137711377
"ID": 946,
1137811378
"Title": "Validate Stack Sequences",
1137911379
"TitleSlug": "validate-stack-sequences",
11380-
"PassRate": "57%",
11380+
"PassRate": "58%",
1138111381
"Difficulty": "Medium",
1138211382
"IsAccepted": true,
1138311383
"IsPaid": false,
@@ -11437,7 +11437,7 @@
1143711437
"ID": 951,
1143811438
"Title": "Flip Equivalent Binary Trees",
1143911439
"TitleSlug": "flip-equivalent-binary-trees",
11440-
"PassRate": "65%",
11440+
"PassRate": "64%",
1144111441
"Difficulty": "Medium",
1144211442
"IsAccepted": true,
1144311443
"IsPaid": false,
@@ -11677,7 +11677,7 @@
1167711677
"ID": 971,
1167811678
"Title": "Flip Binary Tree To Match Preorder Traversal",
1167911679
"TitleSlug": "flip-binary-tree-to-match-preorder-traversal",
11680-
"PassRate": "43%",
11680+
"PassRate": "42%",
1168111681
"Difficulty": "Medium",
1168211682
"IsAccepted": true,
1168311683
"IsPaid": false,
@@ -11749,7 +11749,7 @@
1174911749
"ID": 977,
1175011750
"Title": "Squares of a Sorted Array",
1175111751
"TitleSlug": "squares-of-a-sorted-array",
11752-
"PassRate": "71%",
11752+
"PassRate": "72%",
1175311753
"Difficulty": "Easy",
1175411754
"IsAccepted": true,
1175511755
"IsPaid": false,
@@ -12277,7 +12277,7 @@
1227712277
"ID": 1021,
1227812278
"Title": "Remove Outermost Parentheses",
1227912279
"TitleSlug": "remove-outermost-parentheses",
12280-
"PassRate": "75%",
12280+
"PassRate": "74%",
1228112281
"Difficulty": "Easy",
1228212282
"IsAccepted": true,
1228312283
"IsPaid": false,
@@ -12397,7 +12397,7 @@
1239712397
"ID": 1031,
1239812398
"Title": "Maximum Sum of Two Non-Overlapping Subarrays",
1239912399
"TitleSlug": "maximum-sum-of-two-non-overlapping-subarrays",
12400-
"PassRate": "55%",
12400+
"PassRate": "54%",
1240112401
"Difficulty": "Medium",
1240212402
"IsAccepted": true,
1240312403
"IsPaid": false,
@@ -12409,7 +12409,7 @@
1240912409
"ID": 1032,
1241012410
"Title": "Stream of Characters",
1241112411
"TitleSlug": "stream-of-characters",
12412-
"PassRate": "42%",
12412+
"PassRate": "41%",
1241312413
"Difficulty": "Hard",
1241412414
"IsAccepted": true,
1241512415
"IsPaid": false,
@@ -12613,7 +12613,7 @@
1261312613
"ID": 1049,
1261412614
"Title": "Last Stone Weight II",
1261512615
"TitleSlug": "last-stone-weight-ii",
12616-
"PassRate": "39%",
12616+
"PassRate": "40%",
1261712617
"Difficulty": "Medium",
1261812618
"IsAccepted": true,
1261912619
"IsPaid": false,
@@ -12769,7 +12769,7 @@
1276912769
"ID": 1062,
1277012770
"Title": "Longest Repeating Substring",
1277112771
"TitleSlug": "longest-repeating-substring",
12772-
"PassRate": "51%",
12772+
"PassRate": "50%",
1277312773
"Difficulty": "Medium",
1277412774
"IsAccepted": false,
1277512775
"IsPaid": true,
@@ -13129,7 +13129,7 @@
1312913129
"ID": 1092,
1313013130
"Title": "Shortest Common Supersequence ",
1313113131
"TitleSlug": "shortest-common-supersequence",
13132-
"PassRate": "47%",
13132+
"PassRate": "48%",
1313313133
"Difficulty": "Hard",
1313413134
"IsAccepted": true,
1313513135
"IsPaid": false,
@@ -13333,7 +13333,7 @@
1333313333
"ID": 1109,
1333413334
"Title": "Corporate Flight Bookings",
1333513335
"TitleSlug": "corporate-flight-bookings",
13336-
"PassRate": "47%",
13336+
"PassRate": "48%",
1333713337
"Difficulty": "Medium",
1333813338
"IsAccepted": true,
1333913339
"IsPaid": false,
@@ -13621,7 +13621,7 @@
1362113621
"ID": 1133,
1362213622
"Title": "Largest Unique Number",
1362313623
"TitleSlug": "largest-unique-number",
13624-
"PassRate": "71%",
13624+
"PassRate": "72%",
1362513625
"Difficulty": "Easy",
1362613626
"IsAccepted": false,
1362713627
"IsPaid": true,
@@ -13645,7 +13645,7 @@
1364513645
"ID": 1135,
1364613646
"Title": "Connecting Cities With Minimum Cost",
1364713647
"TitleSlug": "connecting-cities-with-minimum-cost",
13648-
"PassRate": "46%",
13648+
"PassRate": "47%",
1364913649
"Difficulty": "Medium",
1365013650
"IsAccepted": false,
1365113651
"IsPaid": true,
@@ -13686,7 +13686,7 @@
1368613686
"IsAccepted": false,
1368713687
"IsPaid": false,
1368813688
"IsFavor": false,
13689-
"IsNew": true,
13689+
"IsNew": false,
1369013690
"HasNoGoOption": false
1369113691
},
1369213692
{
@@ -13698,19 +13698,19 @@
1369813698
"IsAccepted": false,
1369913699
"IsPaid": false,
1370013700
"IsFavor": false,
13701-
"IsNew": true,
13701+
"IsNew": false,
1370213702
"HasNoGoOption": false
1370313703
},
1370413704
{
1370513705
"ID": 1140,
1370613706
"Title": "Stone Game II",
1370713707
"TitleSlug": "stone-game-ii",
13708-
"PassRate": "61%",
13708+
"PassRate": "60%",
1370913709
"Difficulty": "Medium",
1371013710
"IsAccepted": false,
1371113711
"IsPaid": false,
1371213712
"IsFavor": false,
13713-
"IsNew": true,
13713+
"IsNew": false,
1371413714
"HasNoGoOption": false
1371513715
},
1371613716
{
@@ -13741,7 +13741,7 @@
1374113741
"ID": 1143,
1374213742
"Title": "Longest Common Subsequence",
1374313743
"TitleSlug": "longest-common-subsequence",
13744-
"PassRate": "54%",
13744+
"PassRate": "53%",
1374513745
"Difficulty": "Medium",
1374613746
"IsAccepted": false,
1374713747
"IsPaid": false,

0 commit comments

Comments
 (0)