Medium

Quiz

#227 Basic Calculator II

APPROACH

A string s encodes an arithmetic expression. Evaluate it and return the integer result. Integer division must truncate toward zero.

The expression is always valid, and all intermediate values fit in [-231, 231 - 1].

Note: You may not use any built-in function that evaluates strings as code (e.g., eval()).

Example 1:

Input: s = "3+2*2"
Output: 7

Example 2:

Input: s = " 3/2 "
Output: 1

Example 3:

Input: s = " 3+5 / 2 "
Output: 5
1 of 4
1:00

What is the optimal approach for this problem?