A spreadsheet stores formulas in postfix form, where each operator comes after its two operands. For example ["4", "2", "-", "5", "*"] means (4 - 2) * 5. Postfix needs no parentheses.
Write runFormula(tokens) that evaluates the formula and returns the result. Tokens are integers (possibly negative, like "-7") or one of +, -, *, /. Division between integers truncates toward zero. The formula is always valid and never divides by zero.
Input: tokens = ["4","2","-","5","*"] Output: 10
(4 - 2) * 5 = 10.
Input: tokens = ["4","13","5","/","+"] Output: 6
13 / 5 truncates to 2, and 4 + 2 = 6.
1 <= tokens.length <= 5 * 10^5Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #280 Run a Postfix Spreadsheet Formula. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.