# LeetCode: Evaluate Reverse Polish Notation Solution

`.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}/**2 * @param {string[]} tokens3 * @return {number}4 */5var evalRPN = function (tokens) {6  const s = []7  let a, b8  for (const token of tokens) {9    switch (token) {10      case "+": {11        ;[a, b] = [s.pop(), s.pop()]12        s.push(a + b)13        break14      }15      case "-": {16        ;[a, b] = [s.pop(), s.pop()]17        s.push(b - a)18        break19      }20      case "*": {21        ;[a, b] = [s.pop(), s.pop()]22        s.push(a * b)23        break24      }25      case "/": {26        ;[a, b] = [s.pop(), s.pop()]27        s.push(Math.trunc(b / a))28        break29      }30      default: {31        s.push(Number(token))32      }33    }34  }35  return s.pop()36}`

leetcode

stack

## Next Post

LeetCode: Partitioning Into Minimum Number Of Deci Binary Numbers

May 27, 2021

Search Posts