文章目录
一、基本计算器Ⅰ
题目链接
题目描述:
示例 1:
示例 2:
示例 3:
提示:
思路分析
用两个栈:
nums
:存数字字符
ops
:存符号字符
从前向后遍历字符串,因为这道题只有+/-
,所以不用考虑符号优先级问题。
遍历过程有以下几种情况:
关于首字符带符号的处理:
可以先往nums中加一个0元素,这样-
就可以算进去。
关于(+
和(-
的处理:
每次遇到+/-
的时候判断前一个字符是否是(
,如果是就往nums中添加0。
关于空格的处理:
这里不能遇到空格后跳过,因为可能出现"1-( -2)"
这种情况,所以先预处理字符串把空格全部消掉。
代码如下:
class Solution {
public:
stack<int> nums;
stack<char> ops;
unordered_map<char, function<int(int, int)>> hash = {
{'+', [](int x, int y){return x + y;}},
{'-', [](int x, int y){return x - y;}},
};
void delBlack(string& s)
{
auto it = s.find(" ");
while(it != -1)
{
s.replace(it, 1, "");
it = s.find(" ");
}
}
void calc()
{
if(nums.size() < 2 || ops.empty()) return;
int right = nums.top();
nums.pop();
int left = nums.top();
nums.pop();
char op = ops.top();
ops.pop();
nums.push(hash[op](left, right));
}
int calculate(string s) {
// 去掉空格
delBlack(s);
// 防止首元素为-
nums.push(0);
int n = s.size();
for(int i = 0; i < n; i++)
{
if(isdigit(s[i]))// 数字字符
{
int cur = 0, j = i;
while(j < n && isdigit(s[j]))
{
cur = cur * 10 + (s[j++] - '0');
}
nums.push(cur);
i = j - 1;
}
else// 符号字符
{
if(s[i] == '(') ops.push(s[i]);
else if(hash.count(s[i]))// + -
{
// "(+"情况
if (i > 0 && (s[i - 1] == '('))
{
nums.push(0);
}
// 入栈前先把前面的计算了
while(ops.size() && ops.top() != '(')
{
calc();
}
ops.push(s[i]);
}
else// ')'
{
// 一直算到 '('
while(ops.size() && ops.top() != '(')
{
calc();
}
ops.pop();
}
}
}
while(!ops.empty())
calc();
return nums.top();
}
};
二、基本计算器Ⅱ
题目链接
题目描述:
示例 1:
示例 2:
示例 3:
提示:
思路分析:
这道题跟上面一道题多了一个符号优先级问题,为了解决这个问题,可以加入一个符号优先级表:
unordered_map<char,int> oper_pri = {
{'+',1},
{'-',1},
{'*',2},
{'/',2},
};
当遇到符号+ - * /
的时候先判断优先级:oper_pri[ops.top()] >= oper_pri[s[i]]
的时候说明可以计算前面的表达式了,先计算前面的,然后再把符号添加进ops中。
那遇到)
也要计算前面的,需不需要判断优先级呢?
不需要,因为再()
内部已经自动处理完了。
代码如下:
class Solution {
public:
stack<int> nums;
stack<char> ops;
unordered_map<char, function<int(int, int)>> hash = {
{'+', [](int x, int y){return x + y;}},
{'-', [](int x, int y){return x - y;}},
{'*', [](int x, int y){return x * y;}},
{'/', [](int x, int y){return x / y;}},
};
unordered_map<char,int> oper_pri = {
{'+',1},
{'-',1},
{'*',2},
{'/',2},
};
void delBlack(string& s)
{
auto it = s.find(" ");
while(it != -1)
{
s.replace(it, 1, "");
it = s.find(" ");
}
}
void calc()
{
if(nums.size() < 2 || ops.empty()) return;
int right = nums.top();
nums.pop();
int left = nums.top();
nums.pop();
char op = ops.top();
ops.pop();
nums.push(hash[op](left, right));
}
int calculate(string s) {
// 去掉空格
delBlack(s);
// 防止首元素为-
nums.push(0);
int n = s.size();
for(int i = 0; i < n; i++)
{
if(isdigit(s[i]))// 数字字符
{
int cur = 0, j = i;
while(j < n && isdigit(s[j]))
{
cur = cur * 10 + (s[j++] - '0');
}
nums.push(cur);
i = j - 1;
}
else// 符号字符
{
if(s[i] == '(') ops.push(s[i]);
else if(hash.count(s[i]))// + - * /
{
// "(+"情况
if (i > 0 && (s[i - 1] == '('))
{
nums.push(0);
}
// 入栈前先把前面的计算了
while(ops.size() && ops.top() != '(' && oper_pri[ops.top()] >= oper_pri[s[i]])
{
calc();
}
ops.push(s[i]);
}
else// ')'
{
// 一直算到 '('
while(ops.size() && ops.top() != '(')
{
calc();
}
ops.pop();
}
}
}
while(!ops.empty())
calc();
return nums.top();
}
};