题解 | #牛的表达式计算器#
牛的表达式计算器
https://www.nowcoder.com/practice/261e7f01438f414c92f59c0059d3a906
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param tokens string字符串vector * @return int整型 */ int calculatePostfix(vector<string>& tokens) { // write code here stack<int> st; for (int i = 0;i < tokens.size();++i) { if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") { if (!st.empty()) { int a = st.top(); st.pop(); int b = st.top(); st.pop(); if (tokens[i] == "+") { st.push(b + a); } else if (tokens[i] == "-") { st.push(b - a); } else if (tokens[i] == "*") { st.push(b * a); } else if (tokens[i] == "/") { st.push(b / a); } } } else { st.push(stoi(tokens[i])); } } return st.top(); } };