Skip to main content

Basic Calculator III

Hard
Data Structures and Algorithms
Editorial
Topics:
Math
String
Stack

You are given a string s representing a valid mathematical expression. The expression may contain non-negative integers, the operators '+', '-', '*', '/', parentheses '(', ')', and spaces.

Evaluate the expression and return its result as an integer.

Note that integer division should truncate toward zero. You are not allowed to use any built-in function that evaluates strings as mathematical expressions (such as eval()).

Constraints:

  • 1 <= s.length <= 10^4
  • s consists of digits, operators '+', '-', '*', '/', parentheses '(', ')', and spaces.
  • The given expression is always valid.

Return the integer result of evaluating the expression.

Need help or want more practice?

Practise in an interviewGet AI Coach help