-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathevaluate_reverse_polish_notation.dart
145 lines (120 loc) · 3.13 KB
/
evaluate_reverse_polish_notation.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
-* 150. Evaluate Reverse Polish Notation *-
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, and /. Each operand may be an integer or another expression.
Note that division between two integers should truncate toward zero.
It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.
Example 1:
Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Example 2:
Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Example 3:
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
Constraints:
1 <= tokens.length <= 104
tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200].
*/
import 'dart:collection';
class A {
int evalRPN(List<String> tokens) {
late int a, b;
List<int> s = [];
for (String token in tokens) {
try {
s.add(int.parse(token));
} catch (e) {
a = s.removeLast();
b = s.removeLast();
switch (token) {
case "+":
s.add(b + a);
break;
case "-":
s.add(b - a);
break;
case "*":
s.add(b * a);
break;
case "/":
s.add(b ~/ a);
break;
}
}
}
return s.removeLast();
}
}
class B {
int evalRPN(List<String> tokens) {
if (tokens.isEmpty || tokens.length == 0) {
return 0;
}
int len = tokens.length;
List<int> result = List.filled(len, 0);
int pointer = 0;
for (int i = 0; i < len; i++) {
String current = tokens[i];
if (isDigit(current[current.length - 1])) {
result[pointer++] = int.parse(current);
} else {
if (current == "+") {
result[pointer - 2] += result[--pointer];
} else if (current == "-") {
result[pointer - 2] -= result[--pointer];
} else if (current == "*") {
result[pointer - 2] *= result[--pointer];
} else {
result[pointer - 2] ~/= result[--pointer];
}
}
}
return result[0];
}
bool isDigit(String s) {
if (s.isEmpty) {
return false;
}
return int.tryParse(s) != null;
}
}
class C {
int evalRPN(List<String> tokens) {
List<int> s = [];
HashSet<String> op = HashSet()..addAll(["+", "-", "*", "/"]);
for (String c in tokens) {
if (op.contains(c)) {
int p2 = s.removeLast();
int p1 = s.removeLast();
s.add(execOp(p1, p2, c));
} else {
s.add(int.parse(c));
}
}
return s.removeLast();
}
int execOp(int p1, int p2, String op) {
switch (op) {
case "+":
return p1 + p2;
case "-":
return p1 - p2;
case "*":
return p1 * p2;
case "/":
return p1 ~/ p2;
}
return 0;
}
}