LeetCode 题解(2):Evaluate Reverse Polish Notation

题目:

Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +, -, *, /. Each operand may be an integer or another expression.

Some examples:

  ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
  ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6

题解:

using namespace std;

class Solution {
public:
    int evalRPN(vector<string> &tokens) {
        assert(!tokens.empty());
        stack<int> operand;
        for(vector<string>::iterator iter = tokens.begin(); iter < tokens.end(); iter++)
        {
			const char* p = (*iter).c_str();
            if(p[1] == '\0' && ( p[0] == '+' || p[0] == '-' || p[0] == '*' || p[0] == '/' ))
            {
                switch(*p)
                {
                    case 43:
                    {
                        assert(operand.size() > 1);
                        int operand2 = operand.top();
                        operand.pop();
                        int operand1 = operand.top();
                        operand.pop();
                        operand.push(operand1 + operand2);
    					break;
                    }
                    case 45:
                    {
                        assert(operand.size() > 1);
                        int operand2 = operand.top();
                        operand.pop();
                        int operand1 = operand.top();
                        operand.pop();
                        operand.push(operand1 - operand2);
    					break;
                    }
                    case 42:
                    {
                        assert(operand.size() > 1);
                        int operand2 = operand.top();
                        operand.pop();
                        int operand1 = operand.top();
                        operand.pop();
                        operand.push(operand1 * operand2); 
    					break;
                    }
                    case 47:
                    {
                        assert(operand.size() > 1);
                        int operand2 = operand.top();
                        operand.pop();
                        int operand1 = operand.top();
                        operand.pop();
                        operand.push(operand1 / operand2);
    					break;
                    }
                    default:
                    {
    					break;
                    }
                }
            }
            else
            {
                operand.push(atoi(p));
            }
        }
        assert(operand.size() == 1);
        return operand.top();
    }
};

测试用例:

#include <vector>
#include <exception>
#include <stack>
#include <iostream>

int main(int argc, char* argv [])
{
	string a[] = {"-78","-33","196","+","-19","-","115","+","-","-99","/","-18","8","*","-86","-","-","16","/","26","-14","-","-","47","-","101","-","163","*","143","-","0","-","171","+","120","*","-60","+","156","/","173","/","-24","11","+","21","/","*","44","*","180","70","-40","-","*","86","132","-84","+","*","-","38","/","/","21","28","/","+","83","/","-31","156","-","+","28","/","95","-","120","+","8","*","90","-","-94","*","-73","/","-62","/","93","*","196","-","-59","+","187","-","143","/","-79","-89","+","-"};
	vector<string> stringArray(a, a + sizeof(a)/sizeof(a[0]));
	Solution s;
	cout << s.evalRPN(stringArray) << endl;
	return 0;
}



你可能感兴趣的:(Algorithm,LeetCode,reverse,notation,Polish)