首页 > 其他 > 详细

LeetCode | Evaluate Reverse Polish Notation

时间:2014-04-11 00:04:05      阅读:532      评论:0      收藏:0      [点我收藏+]

The algorithm for evaluating any postfix expression is fairly straightforward:

While there are input tokens left

    • Read the next token from input.
    • If the token is a value
      • Push it onto the stack.
    • Otherwise, the token is an operator (operator here includes both operators and functions).
      • It is known a priori that the operator takes n arguments.
      • If there are fewer than n values on the stack
        • (Error) The user has not input sufficient values in the expression.
      • Else, Pop the top n values from the stack.
      • Evaluate the operator, with the values as arguments.
      • Push the returned results, if any, back onto the stack.
  • If there is only one value in the stack
    • That value is the result of the calculation.
  • Otherwise, there are more values in the stack
    • (Error) The user input has too many values.

LeetCode | Evaluate Reverse Polish Notation,布布扣,bubuko.com

LeetCode | Evaluate Reverse Polish Notation

原文:http://www.cnblogs.com/linyx/p/3656406.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!