-
Notifications
You must be signed in to change notification settings - Fork 8
/
Solution.js
59 lines (56 loc) · 1.45 KB
/
Solution.js
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
/**
* Created by InnF on 2018/2/3.
*/
/**
* 20 / 20 test cases passed.
* Status: Accepted
* Runtime: 110 ms
* @param tokens
* @returns {*}
*/
var evalRPN = function (tokens) {
let vals = [];
for (let t of tokens) {
let v = Number;
if (t === '+') {
v = vals.pop();
vals.push(vals.pop() + v);
} else if (t === '-') {
v = vals.pop();
vals.push(vals.pop() - v);
} else if (t === '*') {
v = vals.pop();
vals.push(vals.pop() * v);
} else if (t === '/') {
v = vals.pop();
vals.push(parseInt(vals.pop() / v));
} else {
vals.push(Number(t));
}
}
return vals.pop();
};
/**
* 20 / 20 test cases passed.
* Status: Accepted
* Runtime: 102 ms
* @param tokens
* @returns {*}
*/
var evalRPN2 = function (tokens) {
let ops = {
'+': function (x, y) { return y + x },
'-': function (x, y) { return y - x },
'*': function (x, y) { return y * x },
'/': function (x, y) { return parseInt(y / x) }
};
let vals = [];
for (let t of tokens) {
if (t in ops) vals.push(ops[t](vals.pop(), vals.pop()));
else vals.push(Number(t));
}
return vals.pop();
};
console.log(evalRPN2(["2", "1", "+", "3", "*"]));
console.log(evalRPN2(["4", "13", "5", "/", "+"]));
console.log(evalRPN2(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]));