The string s is an arithmetic expression made of non-negative whole numbers, the four operators +, -, * and /, and round brackets. Work out its value and return it.
The usual rules of arithmetic apply:
* and / bind tighter than + and -.12/2*3 is (12/2)*3./ is whole-number division that drops the fractional part, rounding toward zero: 9/4 is 2, and (0-9)/4 is -2, not -3.Evaluate it yourself: don't hand the string to a built-in that evaluates expressions, such as eval().
Division and multiplication come first, left to right: 18/5 = 3 (the .6 is dropped), then 3*2 = 6. Last, 4 + 6 = 10.
The brackets give -7 and 6. -7/2 is -3.5, which rounds toward zero to -3. Then 3*6 = 18, and -3 + 18 = 15.
The innermost bracket is 4, so the outer one is 3*4 = 12. Then 60/12 = 5 and 5 - 7 = -2.
Constraints
1 <= s.length <= 10⁴s is made of digits, +, -, *, /, ( and ). It holds no spaces.s is a valid expression: brackets match, no bracket is empty, and every operator stands between two values. There is no unary minus or plus: -3 and (-3) never appear.s, every intermediate result and the answer fit in a signed 32-bit integer. Within any sum, the terms being added or subtracted can be combined in any order without leaving that range.Evaluate one bracket level the way you would a flat expression: keep a list of terms to add up at the end. + and - start a new term (negated for -), and * and / change the last term in place, which gives them their higher precedence for free.
10