Skip to content

Commit 079c082

Browse files
authored
Create 150-evaluate-reverse-polish-notation.js
1 parent 2555904 commit 079c082

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string[]} tokens
3+
* @return {number}
4+
*/
5+
const evalRPN = function(tokens) {
6+
const stack = []
7+
for (let token of tokens) {
8+
if (token === '+') {
9+
stack.push(stack.pop() + stack.pop())
10+
} else if (token === '-') {
11+
stack.push(-stack.pop() + stack.pop())
12+
} else if (token === '*') {
13+
stack.push(stack.pop() * stack.pop())
14+
} else if (token === '/') {
15+
stack.push(Math.trunc((1 / stack.pop()) * stack.pop()))
16+
} else {
17+
stack.push(parseInt(token))
18+
}
19+
}
20+
return stack[0]
21+
}

0 commit comments

Comments
 (0)