标签:写法 ota public empty for pre str not htm
https://www.cnblogs.com/grandyang/p/4247718.html
逆波兰表达式就是把操作数放前面,把操作符后置的一种写法。用stack解决就好了。
class Solution { public: int evalRPN(vector<string>& tokens) { if(tokens.empty()) return 0; stack<int> s; for(int i = 0;i < tokens.size();i++){ if(tokens[i] != "+" && tokens[i] != "-" && tokens[i] != "*" && tokens[i] != "/") s.push(stoi(tokens[i])); else{ int num1 = s.top(); s.pop(); int num2 = s.top(); s.pop(); int tmp; if(tokens[i] == "+") tmp = num1 + num2; else if(tokens[i] == "-") tmp = num2 - num1; else if(tokens[i] == "*") tmp = num1 * num2; else if(tokens[i] == "/") tmp = num2 / num1; s.push(tmp); } } return s.top(); } };
leetcode 150. Evaluate Reverse Polish Notation
标签:写法 ota public empty for pre str not htm
原文地址:https://www.cnblogs.com/ymjyqsx/p/10925485.html