-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
28 lines (26 loc) · 782 Bytes
/
solution.js
File metadata and controls
28 lines (26 loc) · 782 Bytes
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
/**
* @param {string[]} tokens
* @return {number}
*/
var evalRPN = function(tokens) {
let stack = []
tokens.forEach(token => {
if (['+', '-', '*', '/'].indexOf(token) !== -1) {
let value2 = parseInt(stack.pop()),
value1 = parseInt(stack.pop()),
value = 0
if (token == '+')
value = value1 + value2
else if (token == '-')
value = value1 - value2
else if (token == '*')
value = value1 * value2
else if (token == '/')
value = parseInt(value1 / value2)
stack.push(value)
} else {
stack.push(token)
}
})
return stack.length > 0 ? parseInt(stack[0]) : 0
};