ITPub博客

首页 > > > [LeetCode] Evaluate Reverse Polish Notation

[LeetCode] Evaluate Reverse Polish Notation

作者:Ruiyuan Li 时间:2015-04-15 00:00:00 0 删除 编辑

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

解题思路:

后缀表达式计算的问题,做过计算机的同学应该都知道。遇到数字,进栈;遇到操作符,将前面两个操作数出栈,计算,将结果入栈。最后的结果会在栈中。

代码如下:

class Solution {
public:
    int evalRPN(vector& tokens) {
        stack s;
        int len = tokens.size();
        int num1, num2;
        for(int i = 0; i


<!-- -->

原文作者:;如有侵权,请联系管理员删除。

下一篇: windows 8 中文乱码
请登录后发表评论 登录
全部评论

注册时间:2020-05-21

  • 博文量
    495
  • 访问量
    4826