Before storing a formula, a spreadsheet converts it from the usual infix form (operators between operands) into postfix form (each operator after its operands), which is easy to evaluate later.
Write toPostfix(tokens) that takes the infix tokens (non-negative integers as strings, +, -, *, /, (, )) and returns the postfix tokens. * and / bind more tightly than + and -, operators of equal precedence group left to right, and parentheses don't appear in the output. For example 3 + 4 * 2 becomes 3 4 2 * +.
Input: tokens = ["3","+","4","*","2"] Output: ["3","4","2","*","+"]
* binds first, so it comes right after its operands 4 2.
Input: tokens = ["(","3","+","4",")","*","2"] Output: ["3","4","+","2","*"]
The parentheses make + happen first.
1 <= tokens.length <= 10^4Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #282 Convert a Formula to Postfix. 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.