849. Basic Calculator III
class Solution {
public:
/**
* @param s: the expression string
* @return: the answer
*/
int calculate(string s) {
//网上答案
int n = s.size(), num = 0, curRes = 0, res = 0;
char op = '+';
for (int i = 0; i < n; ++i) {
char c = s[i];
if (c >= '0' && c <= '9') {
num = num * 10 + c - '0';
} else if (c == '(') {
int j = i, cnt = 0;
for (; i < n; ++i) {
if (s[i] == '(') ++cnt;
if (s[i] == ')') --cnt;
if (cnt == 0) break;
}
num = calculate(s.substr(j + 1, i - j - 1)); // recursion
}
if (c == '+' || c == '-' || c == '*' || c == '/' || i == n - 1) { // c 和 op的使用有些绕
switch (op) {
case '+': curRes += num; break;
case '-': curRes -= num; break;
case '*': curRes *= num; break;
case '/': curRes /= num; break;
}
if (c == '+' || c == '-' || i == n - 1) {
res += curRes;
curRes = 0;
}
op = c;
num = 0;
}
}
return res;
}
};
Comments
Post a Comment