Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, +
, -
, *
, /
operators and empty spaces ``. The integer division should truncate toward zero.
Example 1:
1 | Input: "3+2*2" |
Example 2:
1 | Input: " 3/2 " |
Example 3:
1 | Input: " 3+5 / 2 " |
Note:
- You may assume that the given expression is always valid.
- Do not use the
eval
built-in library function.
解法1:
使用两个栈,一个栈用来装操作数,另一个用来装操作符。通过后缀表达式的形式来计算。
AC代码
1 | class Solution { |
相关题目
参考文献
[1]后缀表达式:https://www.cnblogs.com/chensongxian/p/7059802.html