150. Evaluate Reverse Polish Notation 發表於 2023-02-13 | 分類於 leetcode problemsolution12345678910111213141516171819202122class Solution {public: int evalRPN(vector<string>& tokens) { stack<int> sta; int ret= 0; for(string str:tokens ){ if(str == "+" || str == "-" || str=="*" || str=="/"){ int a = sta.top(); sta.pop(); int b = sta.top(); sta.pop(); if(str == "+") sta.push(b+a); else if(str =="-") sta.push(b-a); else if(str=="*") sta.push(b*a); else if(str=="/") sta.push(b/a); } else{ sta.push(stoi(str)); } } return sta.top(); }}; analysis time complexity O(n) space complexit O(n)