forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
133 lines (106 loc) · 3.53 KB
/
main.cpp
File metadata and controls
133 lines (106 loc) · 3.53 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/// Source : https://leetcode.com/problems/basic-calculator-iii/description/
/// Author : liuyubobobo
/// Time : 2018-09-07
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
#include <cassert>
using namespace std;
/// Two Stacks
/// Shunting-Yard Algorithms: https://en.wikipedia.org/wiki/Shunting-yard_algorithm
///
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
private:
unordered_set<char> opset = {'+', '-', '*', '/', '(', ')'};
public:
int calculate(string s) {
if(s == "")
return 0;
vector<int> nums;
vector<char> ops;
for(int i = 0; i < s.size(); i ++) {
if(s[i] == ' ')
continue;
if(opset.count(s[i])){
if(s[i] == ')'){
vector<int> tnums = {nums.back()};
nums.pop_back();
vector<char> tops;
while(ops.back() != '('){
tnums.push_back(nums.back());
nums.pop_back();
tops.push_back(ops.back());
ops.pop_back();
}
reverse(tnums.begin(), tnums.end());
reverse(tops.begin(), tops.end());
nums.push_back(order_calc(tnums, tops));
assert(ops.back() == '(');
ops.pop_back();
if(!ops.empty() && (ops.back() == '*' || ops.back() == '/'))
calcTwo(nums, ops);
assert(ops.empty() || ops.back() == '+' || ops.back() == '-' || ops.back() == '(');
}
else
ops.push_back(s[i]);
}
else{
int num = s[i] - '0';
int j;
for(j = i + 1; j < s.size() && isdigit(s[j]); j ++)
num = num * 10 + (s[j] - '0');
i = j - 1;
nums.push_back(num);
if(!ops.empty() && (ops.back() == '*' || ops.back() == '/'))
calcTwo(nums, ops);
}
}
return order_calc(nums, ops);
}
private:
void calcTwo(vector<int>& nums, vector<char>& ops){
assert(nums.size() >= 2);
int second = nums.back();
nums.pop_back();
int first = nums.back();
nums.pop_back();
char op = ops.back();
ops.pop_back();
nums.push_back(calc(first, second, op));
return;
}
int calc(int a, int b, char op){
switch(op){
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
default: assert(false); return -1;
}
}
int order_calc(const vector<int>& nums, const vector<char>& ops){
assert(nums.size() == ops.size() + 1);
int res = nums[0];
for(int i = 0; i < ops.size(); i ++)
res = calc(res, nums[i + 1], ops[i]);
return res;
}
};
int main() {
cout << Solution().calculate("1 + 1") << endl;
// 2
cout << Solution().calculate(" 6-4 / 2 ") << endl;
// 4
cout << Solution().calculate("2*(5+5*2)/3+(6/2+8)") << endl;
// 21
cout << Solution().calculate("(2+6* 3+5- (3*14/7+2)*5)+3") << endl;
// -12
cout << Solution().calculate("(( ( ( 1 * 10 ) -( 3 * 8) ) * 3 ) * 1 )") << endl;
// -42
cout << Solution().calculate("2-4-(8+2-6+(8+4-(1)+8-10))") << endl;
// -15
return 0;
}