Trains the technique from
LeetCode 227Basic Calculator IIThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
Stockroom staff type quantity formulas into a handheld terminal instead of doing the arithmetic themselves. A formula is a string formula holding whole counts joined by the four symbols +, -, * and /, with any number of spaces sprinkled between the pieces.
Evaluate the formula the way the terminal does:
* and / bind tighter than + and -./ keeps only the whole part of the quotient, discarding the fraction, so the result is truncated toward zero.Return the resulting count. You may not hand the string to a language-provided expression evaluator.
Example 1
The tighter symbols run first: `6*3` is 18 and `8/3` truncates to 2, leaving 14 + 18 - 2.
Example 2
Spaces are dropped and `7*4` is folded before the subtraction, so the terminal computes 42 - 28.
Example 3
Equal binding runs left to right: 100/7 truncates to 14, and that 14 is then halved to 7.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def evaluate_formula(formula: str) -> int:public int evaluateFormula(String formula)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.