224. Basic Calculator
class Solution {
public:
int calculate(string s) {
int res = 0, sign = 1, x = 0;
stack<int> nums, ops;
for (char c:s) {
if (isdigit(c)) x = x * 10 + c - '0';
else {
res += sign * x; x = 0;
if (c == '-' || c == '+') sign = (c == '+') ? 1 : -1;
if (c == '(') {
nums.push(res); ops.push(sign);
res = 0; sign = 1;
}
if (c == ')') {
res = nums.top() + ops.top() * res;
nums.pop(); ops.pop();
}
}
}
return res + sign * x;
}
};