Evaluate Reverse Polish Notation Solutions in C++
Number 150
Difficulty Medium
Acceptance 36.4%
Link LeetCode
Other languages Go
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/// Author : Hao Chen// Date : 2014-06-16#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string>#include <vector>#include <iostream>using namespace std;class Solution {public:int evalRPN(vector<string> &tokens) {int i =0;bool err = false;vector<int> exp;for (int i=0; i<tokens.size() && !err; i++ ){if (isNum(tokens[i])) {exp.push_back(value);} else if( isOp(tokens[i])==true ) {if (exp.size() < 2) {return 0; //ERROR}int lhs, rhs;rhs = exp.back();exp.pop_back();lhs = exp.back();exp.pop_back();int evlValue;if (tokens[i]=="+"){evlValue = lhs + rhs;}else if (tokens[i]=="-"){evlValue = lhs - rhs;}else if (tokens[i]=="*"){evlValue = lhs * rhs;}else if (tokens[i]=="/"){evlValue = lhs / rhs;}exp.push_back(evlValue);}else {return 0; //ERROR}}if (exp.size()==1){return exp.back();}return 0;}private:long value;bool isOp(string &op) {return (op=="+" || op=="-" || op=="*" || op=="/");}bool isNum(string &num) {char *end;value = strtol(num.c_str(), &end, 10);if (end == num.c_str() || *end != '\0' || errno == ERANGE){return false;}return true;}};int main(){Solution s;char exps[5][3] = {"42", "9", "6", "-", "+"};vector<string> expression;cout << "Expression: \n ";for (int i=0; i<5; i++){expression.push_back(exps[i]);cout << exps[i] << " ";}cout << endl;cout << s.evalRPN(expression)<<endl;;char exps2[5][3] = {"2", "1", "+", "3", "*"};expression.clear();cout << "Expression: \n ";for (int i=0; i<5; i++){expression.push_back(exps2[i]);cout << exps2[i] << " ";}cout << endl;cout << s.evalRPN(expression)<<endl;return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/evaluate-reverse-polish-notation/description//// Author : liuyubobobo/// Time : 2018-08-29/// Updated: 2019-08-05#include <iostream>#include <stack>#include <vector>using namespace std;/// Using Stacks/// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {public:int evalRPN(vector<string>& tokens) {stack<int> nums;for(const string& s: tokens){if(s == "+" || s == "-" || s == "*" || s == "/"){int a = nums.top();nums.pop();int b = nums.top();nums.pop();if(s == "+")nums.push(b + a);else if(s == "-")nums.push(b - a);else if(s == "*")nums.push(b * a);else if(s == "/")nums.push(b / a);}elsenums.push(atoi(s.c_str()));}return nums.top();}};int main() {return 0;}