-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicCalculatorII.V1.js
More file actions
47 lines (44 loc) · 1.26 KB
/
Copy pathBasicCalculatorII.V1.js
File metadata and controls
47 lines (44 loc) · 1.26 KB
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
// https://leetcode-cn.com/problems/basic-calculator-ii/
var Test = require('./Common/Test');
var calculate = function (s) {
const tokens = s.replace(/ /g, '').replace(/([-/+*])/g, " $1 ").split(' ');
tokens.push('$');
const stack = [];
const calc = (op, a, b) => {
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return Math.trunc(a / b);
}
}
for (const token of tokens) {
if (/\d+/.test(token)) {
const op = stack[stack.length - 1];
if (op != undefined && "*/".includes(op)) {
stack.pop();
stack.push(calc(op, stack.pop(), Number(token)));
}
else {
stack.push(Number(token));
}
}
else {
if ("+-$".includes(token) && stack.length >= 3) {
const num2 = stack.pop();
const op = stack.pop();
const num1 = stack.pop();
stack.push(calc(op, num1, num2));
}
stack.push(token);
}
}
return stack[0];
};
function test(s) {
Test.test(calculate, s);
}
test("3+2*2");
test(" 3/2 ");
test(" 3+5 / 2 ");
test("1+1+1");