-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path224_Basic-Calculator.cpp
More file actions
93 lines (88 loc) · 2.12 KB
/
224_Basic-Calculator.cpp
File metadata and controls
93 lines (88 loc) · 2.12 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
#include <stack>
#include <unordered_map>
#include <string>
#include <cctype>
using namespace std;
class Solution
{
void cal(stack<int> &nums, stack<char> &ops)
{
int a = nums.top();
nums.pop();
int b = nums.top();
nums.pop();
char op = ops.top();
ops.pop();
switch (op)
{
case '+':
nums.push(a + b);
break;
case '-':
nums.push(b - a);
}
}
unordered_map<char, int> hash{
{'+', 1},
{'-', 1}};
public:
int calculate(string s)
{
stack<int> nums;
nums.push(0);
stack<char> ops;
int l = s.size();
for (int i = 0; i < l; ++i)
{
if (s[i] == ' ')
{
continue;
}
if (s[i] == '(')
{
ops.push(s[i]);
while (i + 1 < l && s[i + 1] == ' ')
{
++i;
}
if (s[i + 1] == '-')
{
nums.push(0);
}
continue;
}
if (s[i] == ')')
{
while (ops.top() != '(')
{
cal(nums, ops);
}
ops.pop();
continue;
}
if (isdigit(s[i]))
{
int left = i;
while (i + 1 < l && isdigit(s[i + 1]))
{
++i;
}
nums.push(stoi(s.substr(left, i - left + 1)));
continue;
}
while (!ops.empty() && ops.top() != '(' && hash[ops.top()] >= hash[s[i]])
{
cal(nums, ops);
}
ops.push(s[i]);
}
while (!ops.empty())
{
cal(nums, ops);
}
return nums.top();
}
};
// 需要注意开头为负数
// 注意查找'(' 右第一个非空字符,若为'-' 要想nums栈压入0,方便计算
// 尾部按序计算,直接算最终结果 '-' 会导致结果错误