LeetCode: Evaluate Reverse Polish Notation Solution

1/**
2 * @param {string[]} tokens
3 * @return {number}
4 */
5var evalRPN = function (tokens) {
6 const s = []
7 let a, b
8 for (const token of tokens) {
9 switch (token) {
10 case "+": {
11 ;[a, b] = [s.pop(), s.pop()]
12 s.push(a + b)
13 break
14 }
15 case "-": {
16 ;[a, b] = [s.pop(), s.pop()]
17 s.push(b - a)
18 break
19 }
20 case "*": {
21 ;[a, b] = [s.pop(), s.pop()]
22 s.push(a * b)
23 break
24 }
25 case "/": {
26 ;[a, b] = [s.pop(), s.pop()]
27 s.push(Math.trunc(b / a))
28 break
29 }
30 default: {
31 s.push(Number(token))
32 }
33 }
34 }
35 return s.pop()
36}

Comments

Loading comments...

Tags

leetcode

stack

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Partitioning Into Minimum Number Of Deci Binary Numbers

HoningJS

Search Posts