给你一个字符串数组 tokens
,表示一个根据 逆波兰表示法 表示的算术表达式。
请你计算该表达式。返回一个表示表达式值的整数。
注意:
'+'
、'-'
、'*'
和 '/'
。示例 1:
输入:tokens = ["2","1","+","3","*"]
输出:9
解释:该算式转化为常见的中缀算术表达式为:((2 + 1) * 3) = 9
示例 2:
输入:tokens = ["4","13","5","/","+"]
输出:6
解释:该算式转化为常见的中缀算术表达式为:(4 + (13 / 5)) = 6
示例 3:
输入:tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
输出:22
解释:该算式转化为常见的中缀算术表达式为:
((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
提示:
1 <= tokens.length <= 104
tokens[i]
是一个算符("+"
、"-"
、"*"
或 "/"
),或是在范围 [-200, 200]
内的一个整数逆波兰表达式:
逆波兰表达式是一种后缀表达式,所谓后缀就是指算符写在后面。
( 1 + 2 ) * ( 3 + 4 )
。( ( 1 2 + ) ( 3 4 + ) * )
。逆波兰表达式主要有以下两个优点:
1 2 + 3 4 + *
也可以依据次序计算出正确结果。给定一个逆波兰表达式(Reverse Polish Notation),我们需要计算其结果,我们可以使用栈来存储操作数,遇到运算符时从栈中弹出操作数进行运算,并将结果再次压入栈中,直到遍历完整个表达式。
st
用于存储操作数。st
中。st
中弹出两个操作数(先弹出的是右操作数,后弹出的是左操作数),进行相应的运算,然后将运算结果压入栈 st
中。st
中最后剩下的元素即为计算结果。将栈顶元素返回作为最终结果。class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
for(int i = 0; i < tokens.size(); i++){
if(isdigit(tokens[i][0]) || (tokens[i].size() > 1 && tokens[i][0] == '-')) {
// 如果是数字(可以是负数),将其转换为整数并压入栈中
st.push(stoi(tokens[i]));
} else {
// 如果是运算符,则从栈中弹出两个操作数进行运算,并将结果压入栈中
int op1 = st.top();
st.pop();
int op2 = st.top();
st.pop();
int op_new;
switch(tokens[i][0]){
case '+':
op_new = op2 + op1;
break;
case '-':
op_new = op2 - op1;
break;
case '*':
op_new = op2 * op1;
break;
case '/':
op_new = op2 / op1;
break;
}
st.push(op_new);
}
}
return st.top();
}
};
st
的空间复杂度为 O(N),其中 N 是表达式的长度。在最坏情况下,所有元素都是操作数,需要存储在栈中。除了栈之外,不需要使用额外的空间。因此,整体空间复杂度为 O(N)。逆波兰表达式是一种后缀表达式,与常见的中缀表达式相比,逆波兰表达式的优点是去除了括号,使得表达式无歧义,且适合使用栈进行计算。中缀表达式是我们平时常见的数学表达式,例如 ( 1 + 2 ) * ( 3 + 4 )。
将中缀表达式转换为逆波兰表达式的过程称为中缀转后缀,可以使用栈和优先级规则来完成转换。以下是一个示例代码,演示了如何将中缀表达式转换为逆波兰表达式:
#include
#include
#include
#include
#include
using namespace std;
// 操作符优先级映射表
unordered_map<char, int> priority = {
{'+', 1},
{'-', 1},
{'*', 2},
{'/', 2},
};
// 判断字符是否为操作符
bool isOperator(char c) {
return priority.count(c);
}
// 判断操作符 op1 的优先级是否高于操作符 op2
bool isHigherPriority(char op1, char op2) {
return priority[op1] > priority[op2];
}
// 中缀表达式转逆波兰表达式
vector<string> infixToRPN(const string& infix) {
vector<string> rpn;
stack<char> operators;
int n = infix.size();
int i = 0;
while (i < n) {
char c = infix[i];
if (isspace(c)) {
i++;
continue;
} else if (isdigit(c)) {
// 数字直接加入结果列表
string num;
while (i < n && isdigit(infix[i])) {
num += infix[i];
i++;
}
rpn.push_back(num);
} else if (isOperator(c)) {
// 操作符的处理
while (!operators.empty() && operators.top() != '(' && isHigherPriority(operators.top(), c)) {
rpn.push_back(string(1, operators.top()));
operators.pop();
}
operators.push(c);
i++;
} else if (c == '(') {
// 左括号直接入栈
operators.push(c);
i++;
} else if (c == ')') {
// 右括号的处理
while (!operators.empty() && operators.top() != '(') {
rpn.push_back(string(1, operators.top()));
operators.pop();
}
operators.pop(); // 弹出左括号
i++;
} else {
// 非法字符
cout << "Invalid character: " << c << endl;
return {};
}
}
while (!operators.empty()) {
rpn.push_back(string(1, operators.top()));
operators.pop();
}
return rpn;
}
int main() {
string infix = "( 1 + 2 ) * ( 3 + 4 )";
vector<string> rpn = infixToRPN(infix);
for (const string& token : rpn) {
cout << token << " ";
}
cout << endl;
return 0;
}
以上代码将中缀表达式 ( 1 + 2 ) * ( 3 + 4 ) 转换为逆波兰表达式 1 2 + 3 4 + *。