Leetcode 之Evaluate Reverse Polish Notation(41)
生活随笔
收集整理的這篇文章主要介紹了
Leetcode 之Evaluate Reverse Polish Notation(41)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
很簡單的一道題,定義一個棧保留操作數(shù),遇操作符則彈出運算即可。
bool isOperator(string &op){//注意用法return op.size() == 1 && string("+-*/").find(op) != string::npos;}int evalRPN(vector<string> &tokens){stack<string> s;for (auto token : tokens){if (!isOperator(token)){//如果是操作數(shù),則入棧 s.push(token);}else{//如果是操作符,則彈出操作數(shù)進行運算int y = stoi(s.top());s.pop();int x = stoi(s.top());s.pop();if (token == "+")x += y;if (token == "-")x -= y;if (token == "*")x *= y;if (token == "/")x /= y;s.push(to_string(x));}}return stoi(s.top());} View Code?
轉(zhuǎn)載于:https://www.cnblogs.com/573177885qq/p/5537374.html
總結(jié)
以上是生活随笔為你收集整理的Leetcode 之Evaluate Reverse Polish Notation(41)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。