Design Gurus Logo
Basic Calculator II (medium)

Problem Statement

Given a string s, representing the mathematical expression, evaluate the expression and return the integer value.

The integer division should truncate toward zero.

*Note: You are not allowed to use the built-in functions like eval().

Examples

Example 1:

  • Input: "8*5/4+3-2"
  • Expected Output: 11
  • Justification: The calculation proceeds as follows: 8*5 = 40, 40/4 = 10, 10+3 = 13, 13-2 = 11.

Example 2:

  • Input: "10+20/5*3"
  • Expected Output: 22
  • Justification: The order of operations gives 20/5 = 4, 4*3 = 12, and 10+12 = 22.

Example 3:

  • Input: "50-25+5*3/2*2-10/2"
  • Expected Output: 34
  • Justification: Following the order of operations: 50 - 25 = 25, (15/2) = 7 (rounded down), 7*2 = 14, 25+14 = 39, 10/2 = 5, and 39 - 5 = 34.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory