 All Problems
Basic Calculator II
medium
stack
string
math
amazon
google
facebook
microsoft

Given a string s representing a valid arithmetic expression with integers and the operators +, -, *, /, evaluate the expression and return its value. The integer division should truncate toward zero.

Example 1:

Input: 3+2*2
Output: 7

Example 2:

Input: 3/2
Output: 1

Example 3:

Input: 3+5/2
Output: 5

Constraints:

  • 1 ≤ s.length ≤ 3 × 10⁵
  • s consists of integers, +, -, *, /, and spaces
  • s represents a valid expression
  • All integers are non-negative, result fits in a 32-bit signed integer

Input format: One line — the arithmetic expression string.

Output format: Integer result.

Run to check your code against the sample cases, or submit to run every case