forked from arkingc/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
227.cpp
46 lines (42 loc) · 1.47 KB
/
227.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
class Solution {
public:
int calculate(string s) {
deque<int> nums;
deque<char> ops;
int i = 0;
for(int j = i;j < s.size();j++){
if(s[j] == '+' || s[j] == '-' || s[j] == '*' || s[j] == '/'){
int num = stoi(s.substr(i,j - i));
if(!ops.empty() && (ops.back() == '*' || ops.back() == '/')){
if(ops.back() == '*')
nums.back() = nums.back() * num;
else
nums.back() = nums.back() / num;
ops.pop_back();
}
else
nums.push_back(num);
ops.push_back(s[j]);
i = j + 1;
}
}
nums.push_back(stoi(s.substr(i,s.size() - i)));//最后一个数
if(!ops.empty() && (ops.back() == '*' || ops.back() == '/')){
int num = nums.back();nums.pop_back();
if(ops.back() == '*')
nums.back() = nums.back() * num;
else
nums.back() = nums.back() / num;
ops.pop_back();
}
while(nums.size() > 1){
int num = nums.front(); nums.pop_front();
if(ops.front() == '+')
nums.front() = num + nums.front();
else if(ops.front() == '-')
nums.front() = num - nums.front();
ops.pop_front();
}
return nums.front();
}
};