-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_stack_n.cpp
50 lines (44 loc) · 1.1 KB
/
AC_stack_n.cpp
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
43
44
45
46
47
48
49
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_stack_n.cpp
* Create Date: 2015-09-07 19:07:22
* Descripton:
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
int calculate(string s) {
stack<int> nums;
stack<int> signs;
int num = 0, res = 0, sign = 1;
for (auto ch : s) {
if (ch == ' ') continue;
if (isdigit(ch)) {
num = num * 10 + (ch - '0');
} else {
res += num * sign;
num = 0;
if (ch == '+') sign = 1;
if (ch == '-') sign = -1;
if (ch == '(') {
nums.push(res);
signs.push(sign);
res = 0;
sign = 1;
}
if (ch == ')') {
res = nums.top() + res * signs.top();
nums.pop();
signs.pop();
}
}
}
res += sign * num;
return res;
}
};
int main() {
return 0;
}