-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-calculator-ii.h
42 lines (34 loc) · 1 KB
/
basic-calculator-ii.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef BASIC_CALCULAOR_II_H_
#define BASIC_CALCULAOR_II_H_
#include <string>
namespace solution {
bool issign(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; }
int calculate(std::string s) {
// without stack
// Runtime: 4 ms, faster than 98.87% of C++ online submissions for Basic Calculator II.
// Memory Usage: 7.8 MB, less than 86.50% of C++ online submissions for Basic Calculator II.
//
auto result{0}, last{0}, current{0};
auto sign = '+';
for (size_t i = 0; i < s.size(); ++i) {
if (std::isdigit(s[i])) {
current = current * 10 + s[i] - '0';
}
if (issign(s[i]) || i == s.size() - 1) {
if (sign == '+' || sign == '-') {
result += last;
last = sign == '+' ? current : -current;
} else if (sign == '*') {
last *= current;
} else if (sign == '/') {
last /= current;
}
sign = s[i];
current = 0;
}
}
result += last;
return result;
}
} // namespace solution
#endif // BASIC_CALCULAOR_II_H_