Skip to content

Commit bc52a7f

Browse files
committed
update
1 parent 72fe3d1 commit bc52a7f

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,3 @@ production
1111

1212
#System
1313
.DS_Store
14-
15-
Solution.java
Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,66 @@
1+
import java.util.*;
2+
13
public class Solution {
2-
public static void main(String[] args) {
3-
System.out.println("Hello world!");
4+
public boolean parseBoolExpr(String expression) {
5+
Deque<Character> deque = new ArrayDeque<>();
6+
for (int i = 0; i < expression.length(); i++) {
7+
// 如果是逗号,则直接忽略
8+
Character curChar = expression.charAt(i);
9+
if (curChar == ',') {
10+
continue;
11+
}
12+
// 如果非右括号,则入栈
13+
if (curChar != ')') {
14+
deque.addLast(curChar);
15+
}else {
16+
// 如果是右括号,则开始计算
17+
Set<Character> set = new HashSet<>();
18+
while(!deque.isEmpty()){
19+
Character peekChar = deque.removeLast();
20+
// 如果等于左括号,则开始计算,否则一直移除
21+
if (peekChar == '(') {
22+
Character operator = deque.removeLast();
23+
if (operator == '!') {
24+
// 则取反
25+
if (set.contains('t')) {
26+
deque.addLast('f');
27+
} else {
28+
deque.addLast('t');
29+
}
30+
} else if (operator == '&') {
31+
// 判断 set 中是否都是 t
32+
if (set.contains('f')) {
33+
deque.addLast('f');
34+
} else {
35+
deque.addLast('t');
36+
}
37+
} else if (operator == '|') {
38+
// 判断 set 中是否有 t
39+
if (set.contains('t')) {
40+
deque.addLast('t');
41+
} else {
42+
deque.addLast('f');
43+
}
44+
}
45+
set.clear();
46+
break;
47+
}else {
48+
//不是左括号且不是逗号,则将运算符放入集合中
49+
if (peekChar != ',') {
50+
set.add(peekChar);
51+
}
52+
}
53+
}
54+
55+
}
56+
}
57+
// 最后弹出栈顶元素
58+
Character result = deque.peekLast();
59+
return result == 't';
60+
}
61+
62+
public static void main(String[] args){
63+
Solution solution = new Solution();
64+
solution.parseBoolExpr("&(|(f))");
465
}
566
}

0 commit comments

Comments
 (0)