forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0224.cpp
32 lines (32 loc) · 782 Bytes
/
0224.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
class Solution
{
public:
int calculate(string s)
{
vector<int> st;
int op = 1, r = 0, l = 0;
for (auto c : s)
{
if (c >= '0' && c <= '9') r = r * 10 + (c - '0');
else if (c == '+' || c == '-')
{
l += op * r;
r = 0, op = c == '+' ? 1 : -1;
}
else if (c == '(')
{
st.push_back(l);
st.push_back(op);
op = 1, l = 0;
}
else if (c == ')')
{
l += op * r;
r = 0;
int tp = st.back(); st.pop_back();
l = tp * l + st.back(); st.pop_back();
}
}
return l + op * r;
}
};