Evaluate Addition and Multiplication
Problem statement
Evaluate a valid arithmetic expression containing nonnegative decimal integers, binary addition +, multiplication x, and optional ASCII spaces around tokens.
Multiplication has higher precedence than addition. Leading zeros are allowed and do not change a decimal integer’s value. Spaces never split the digits of one number. Return the integer result.
The grammar contains no subtraction, division, unary signs or parentheses. There is at least one number, and each binary operator has a number on both sides.
Function
evaluateExpression(expression: String) → intExamples
Example 1
expression = "2+3x4"return = 14Multiplication is evaluated first in this practice grammar: 3x4 is 12, then 2+12 is 14.
Example 2
expression = "10x2+3x4"return = 32The two products are 20 and 12; adding them gives 32.
Example 3
expression = "7"return = 7A single literal evaluates to its own value.
Constraints
1 <= expression.length <= 10^4.- The expression contains only decimal digits, spaces,
+and lowercasexand follows the stated valid grammar. - Every literal, intermediate product, intermediate sum and final result is between
0and2147483647.