Skip to content

Commit bb002fd

Browse files
committed
Solve 224 with C++
1 parent b10178c commit bb002fd

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Author: illuz <iilluzen[at]gmail.com>
3+
* File: AC_stack_n.cpp
4+
* Create Date: 2015-09-07 19:07:22
5+
* Descripton:
6+
*/
7+
8+
#include <bits/stdc++.h>
9+
10+
using namespace std;
11+
const int N = 0;
12+
13+
class Solution {
14+
public:
15+
int calculate(string s) {
16+
stack<int> nums;
17+
stack<int> signs;
18+
int num = 0, res = 0, sign = 1;
19+
for (auto ch : s) {
20+
if (ch == ' ') continue;
21+
if (isdigit(ch)) {
22+
num = num * 10 + (ch - '0');
23+
} else {
24+
res += num * sign;
25+
num = 0;
26+
if (ch == '+') sign = 1;
27+
if (ch == '-') sign = -1;
28+
if (ch == '(') {
29+
nums.push(res);
30+
signs.push(sign);
31+
res = 0;
32+
sign = 1;
33+
}
34+
if (ch == ')') {
35+
res = nums.top() + res * signs.top();
36+
nums.pop();
37+
signs.pop();
38+
}
39+
}
40+
}
41+
res += sign * num;
42+
return res;
43+
}
44+
};
45+
46+
int main() {
47+
48+
return 0;
49+
}
50+

0 commit comments

Comments
 (0)