leetcode-150. 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
  • Subscribe to see which companies asked this question.

思路分析

  • 思路:由于逆波兰表达式本身不需要括号来限制哪个运算该先进行,因此可以直接利用栈来模拟计算:遇到操作数直接压栈,碰到操作符直接取栈顶的2个操作数进行计算(注意第一次取出来的是右操作数),然后再把计算结果压栈,如此循环下去。最后栈中剩下的唯一一个元素便是整个表达式的值
  • 注意除法的除零保护

AC代码:

class Solution {
public:
    int evalRPN(vector<string> &tokens) {
        int ret=0;
        stack<int> s;  //将tokens往里面丢
        int L=0,R=0;  //左右操作树
        for(int i=0;i<tokens.size();i++)
            {
            if(tokens[i]=="+")
                {
                R=s.top(); //先是右操作数
                s.pop();
                L=s.top();
                s.pop();
                ret=L+R;
                s.push(ret);
            }else if(tokens[i]=="-")
                {
                R=s.top();
                s.pop();
                L=s.top();
                s.pop();
                ret=L-R;
                s.push(ret);
            }else if(tokens[i]=="*")
                {
                R=s.top();
                s.pop();
                L=s.top();
                s.pop();
                ret=L*R;
                s.push(ret);
            }else if(tokens[i]=="/")
                {
                R=s.top();
                s.pop();
                L=s.top();
                s.pop();
                if(R!=0)
                    ret=L/R;
                else
                    return -1;
                s.push(ret);
            }else
                {
                s.push(atoi(tokens[i].c_str()));
            }
        }//end for
         
        return s.top();
    }
};

#include<iostream>
#include<math.h>

#include <vector>
#include<string>
#include<deque>
#include <stack>
#include <queue>
#include<map>
#include <set>

using namespace std;


//evluate reverse polish notation
class Solution {
public:
    int str2digit(string str)
    {


        int ret = 0;

        const char *src = str.c_str();
        ret = atoi(src);

        //for (int i = 0; i < str.size();i++)
        //{
        //  if ()
        //  {
        //  }
        //}
    }

    string digit2str(int data)
    {
        string ret;
        char temp[128] = " ";
        sprintf_s(temp, "%d", data); //./solution.h:25:3: error: use of undeclared identifier 'sprintf_s'

        ret = temp;
        return ret;
    }
    int evalRPN(vector<string> &tokens) {
        stack<string> sta;
        int oper1, oper2,oper3;
        for (unsigned int i = 0; i < tokens.size();i++)
        {
            if (tokens[i] == "+")
            {
                oper2 = str2digit(sta.top());
                sta.pop();
                oper1 = str2digit(sta.top());
                sta.pop();

                oper3 = oper1 + oper2;
                sta.push(digit2str(oper3));

            }else if (tokens[i]=="-")
            {
                oper2 = str2digit(sta.top());
                sta.pop();
                oper1 = str2digit(sta.top());
                sta.pop();

                oper3 = oper1 - oper2;
                sta.push(digit2str(oper3));
            }
            else if (tokens[i]=="*")
            {
                oper2 = str2digit(sta.top());
                sta.pop();
                oper1 = str2digit(sta.top());
                sta.pop();

                oper3 = oper1 * oper2;
                sta.push(digit2str(oper3));
            }
            else if (tokens[i]=="/")
            {
                oper2 = str2digit(sta.top());
                sta.pop();
                oper1 = str2digit(sta.top());
                sta.pop();

                oper3 = oper1 / oper2;  //除零保护
                sta.push(digit2str(oper3));
            }
            else
            {
                sta.push(tokens[i]);
            }
        }
        return str2digit(sta.top());
    }
};


int main()
{

    return 0;
}


reference

    原文作者:ranjiewen
    原文地址: https://www.cnblogs.com/ranjiewen/p/6851285.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞