---
# System prepended metadata

title: 0227. Basic Calculator II
tags: [Leetcode, FaceBook, Medium, Stack]

---

# 0227. Basic Calculator II
###### tags: `Leetcode` `FaceBook` `Medium` `Stack`
Link: https://leetcode.com/problems/basic-calculator-ii/
## 思路
**注意在用trim或者replaceAll去掉空格的时候一定要写前面的=**
用Stack
遇到乘号或除号算出结果再压入stack
遇到减号取相反数压入stack
最后stack剩的所有数相加就是结果
**注意line 19不能用if，因为要判断现在是不是已经遍历到了字符串的最后**
## Code
不用栈空间复杂度为$O(1)$的解法 参考官解
```java=
class Solution {
    public int calculate(String s) {
        s = s.replaceAll(" ","");
        s = s.trim();
        char[] arr = s.toCharArray();
        int currNum = 0, lastNum = 0, result = 0;
        char sign = '+';
        int idx = 0;
        while(idx<s.length()){
            if(Character.isDigit(arr[idx])){
                int num = 0;
                while(idx<arr.length && Character.isDigit(arr[idx])){
                    num = num*10+(arr[idx]-'0');
                    idx++;
                }
                idx--;
                currNum = num;
            }
            if(!Character.isDigit(arr[idx])||idx==s.length()-1){
                if(sign=='+'||sign=='-'){
                    result+=lastNum;
                    lastNum = (sign=='+')?currNum:-currNum;
                }
                if(sign=='*'){
                    lastNum = currNum*lastNum;
                }
                if(sign=='/'){
                    lastNum = lastNum/currNum;
                }
                sign = arr[idx];
                currNum = 0;
            }
            idx++;
        }
        result+=lastNum;
        return result;
    }
}
```
$O(N)$ $O(N)$ 解法
```java=
class Solution {
    public int calculate(String s) {
        char[] charArray = s.toCharArray();
        Stack<Integer> stack = new Stack<>();
        int num = 0;
        char preSign = '+';
        for(int i = 0;i < charArray.length;i++){
            if(Character.isDigit(charArray[i])){
                num = num*10+charArray[i]-'0';
            }
            if((!Character.isDigit(charArray[i]) && charArray[i] != ' ')|| i==charArray.length-1){
                switch(preSign){
                    case '+':
                        stack.push(num);
                        break;
                    case '-':
                        stack.push(-num);
                        break;
                    case '*':
//                        System.out.println(stack.peek()+" "+num);
                        stack.push(stack.pop()*num);
                        break;
                    case '/':
                        stack.push(stack.pop()/num);
                        break;
                }
                preSign = charArray[i];
                num = 0;
            }
        }
        int ans = 0;
        while(!stack.empty()){
            ans+=stack.pop();
        }
        return ans;
    }
}
```
## Result
Runtime: 8 ms, faster than **80.04%** of Java online submissions for Basic Calculator II.
Memory Usage: 39 MB, less than **75.40%** of Java online submissions for Basic Calculator II.