For the best experience, increase the window size or view on a laptop or desktop device
Title | ||
|---|---|---|
Loading... | ||
For the best experience, increase the window size or view on a laptop or desktop device
Title | ||
|---|---|---|
Loading... | ||
Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation. The strings only contains + and * operators. Don't have to consider parentheses.
Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().
Example 1:
Input: s = "1 + 1"
Output: 2
Example 2:
Input: s = " 2-1 + 2 "
Output: 3
Now the expression string may contain open ( and closing parentheses ), minus sign -, division sign /, non-negative integers, and empty spaces.
The expression string contains only non-negative integers, +, -, *, / operators, open ( and closing parentheses ), and empty spaces . The integer division should truncate toward zero.
You may assume that the given expression is always valid. All intermediate results will be in the range of [-2147483648, 2147483647].
Examples:
"1 + 1" = 2
" 6-4 / 2 " = 4
"2*(5+5*2)/3+(6/2+8)" = 21
"(2+6* 3+5- (3*14/7+2)*5)+3"=-
Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation. The strings only contains + and * operators. Don't have to consider parentheses.
Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().
Example 1:
Input: s = "1 + 1"
Output: 2
Example 2:
Input: s = " 2-1 + 2 "
Output: 3
Now the expression string may contain open ( and closing parentheses ), minus sign -, division sign /, non-negative integers, and empty spaces.
The expression string contains only non-negative integers, +, -, *, / operators, open ( and closing parentheses ), and empty spaces . The integer division should truncate toward zero.
You may assume that the given expression is always valid. All intermediate results will be in the range of [-2147483648, 2147483647].
Examples:
"1 + 1" = 2
" 6-4 / 2 " = 4
"2*(5+5*2)/3+(6/2+8)" = 21
"(2+6* 3+5- (3*14/7+2)*5)+3"=-
Output