DIY: Basic Calculator III
Solve the interview question "Basic Calculator III" in this lesson.
We'll cover the following
Problem statement
You are given a string s
containing only positive integers, operators ('+'
, '-'
, '*'
, '/'
), and parentheses '()'
. The intermediate results of the given expression string are in the range [-, - 1]. The expression is guaranteed to be valid and the integer division truncates towards zero.
Your task is to evaluate the given expression string.
Constraints
- <=
expression
<= - The string consists of only digits,
'+'
,'-'
,'*'
,'/'
,'('
, and')'
.
Input
The input of the function is a string. The following is an example input:
3*(5+5)/2+(6/2)
Output
The following is the output of the above input:
18
Coding exercise
Implement the calculate(s)
function, where the s
is a mathematical expression and returns the integer number.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.