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