1.简单的加减问题,比如:1+2-3=0
1+2-3 可以看作 +1+2-3 , 那么我们可以逐个把这些数字放到stack,最后加起来
2.如果再加上乘除的问题,比如:1+2-3*4 = -9;
加了乘除后,乘除的优先级要高于加减,因此我们遇到*/的时候,我们先把前面的数组pop出来做完计算再压回stack
3.如果在+-*/基础上增加了括号,比如:5+(3+2-1)*4 = 21
227. Basic Calculator II
Medium
Given a string <code>s</code> which represents an expression, evaluate this expression and return its value.
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 <code>[-231, 231 - 1]</code>.
Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.
Example 1:
Example 2:
Example 3:
Constraints:
<code>1 <= s.length <= 3 * 105</code>
<code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.
<code>s</code> represents a valid expression.
All the integers in the expression are non-negative integers in the range <code>[0, 231 - 1]</code>.
The answer is guaranteed to fit in a 32-bit integer.
849 · Basic Calculator III
Algorithms
Hard
Description
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open <code>(</code> and closing parentheses <code>)</code>, the plus <code>+</code> or minus sign <code>-</code>, <code>non-negative</code> integers and empty spaces .
The expression string contains only non-negative integers, <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code> operators , open <code>(</code> and closing parentheses <code>)</code> 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 <code>[-2147483648, 2147483647]</code>
Do not use the <code>eval</code> built-in library function.
Example