原題:求逆波蘭表達式的值,在逆波蘭表達式中,其有效的運算符號+,-,*,/。每一個運算對象能夠是整數,也能夠是另外一個逆波蘭計數表達。code
["2","1","+","3","*"] -> ((2+1) * 3) -> 9對象
["4","13","5","/","+"] -> (4 + (13 / 5)) -> 6token
int evalRPN(vector<string>& tokens) { stack<string> s; for(auto tok : tokens) { if(!is_operator(tok)) { s.push(tok); } else { int y = stoi(s.top()); s.pop(); int x = stoi(s.top()); s.pop(); if(tok[0] == '+') x += y; else if(tok[0] == '-') x -= y; else if(tok[0] == '*') x *= y; else x /= y; s.push(to_string(x)); } } return stoi(s.top()); } bool is_operator(const string &op) { return op.length() == 1 && string("+-*/").find(op) != string::npos; }