【刷题日记】栈和队列-逆波兰表达式求值-L150-Medium
给你一个字符串数组 tokens ,表示一个根据 逆波兰表示法 表示的算术表达式。
请你计算该表达式。返回一个表示表达式值的整数。
思路
- 用栈,保存数字;遇到运算符,弹出两个数运算后,结果入栈。结束后栈里的最后一个元素为结果。
学习点
- 可以用
std::stoi()、std::stoll()
转换为数字,不需要自己再写。
代码
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
| #include <iostream> #include <vector> #include <cstring> #include <algorithm> #include <stack> #include <map> using namespace std;
class Solution { public: int evalRPN(vector<string>& tokens) { stack<int> operands; map<string, int> operator_map; operator_map.insert(make_pair<>("+", 1)); operator_map.insert(make_pair<>("-", 2)); operator_map.insert(make_pair<>("*", 3)); operator_map.insert(make_pair<>("/", 4));
for (int i = 0; i < tokens.size(); i++) { if (!operator_map.count(tokens[i])) operands.push(stoi(tokens[i])); else { auto op1 = operands.top(); operands.pop(); auto op2 = operands.top(); operands.pop(); int res; switch (operator_map[tokens[i]]) { case 1: res = op2 + op1; operands.push(res); break; case 2: res = op2 - op1; operands.push(res); break; case 3: res = op2 * op1; operands.push(res); break; case 4: res = op2 / op1; operands.push(res); break; } } }
return operands.top(); } };
|