https://leetcode.com/problems/basic-calculator-ii/
- Stack
- String
Scan the expression, keep the last operator, and use a stack to handle multiplication and division immediately.
O(n)
O(n)
import java.util.*;
class Solution {
public int calculate(String s) {
Stack<Integer> stack = new Stack<>();
int num = 0;
char sign = '+';
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isDigit(c)) num = num * 10 + (c - '0');
if ((!Character.isDigit(c) && c != ' ') || i == s.length() - 1) {
if (sign == '+') stack.push(num);
else if (sign == '-') stack.push(-num);
else if (sign == '*') stack.push(stack.pop() * num);
else if (sign == '/') stack.push(stack.pop() / num);
sign = c;
num = 0;
}
}
int ans = 0;
for (int n : stack) ans += n;
return ans;
}
}