leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0227.cpp (465B)


0 class Solution {
1 public:
2 int calculate(string s) {
3 stringstream ss("+" + s);
4 char op;
5 int n, last, ans = 0;
6 while (ss >> op >> n) {
7 if (op == '+' || op == '-') {
8 n = op == '+' ? n : -n;
9 ans += n;
10 } else {
11 n = op == '*' ? last * n : last / n;
12 ans = ans - last + n;
13 }
14 last = n;
15 }
16 return ans;
17 }
18 };