Wednesday, September 17, 2014

Evaluate Reverse Polish Notation

Problem

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

Idea

  • 用stack来解,维护这个栈存储所有的number,如果碰到了操作符"+","-","*","/"则弹出两个栈里的数分别做右操作数和左操作数,然后把计算结果再压栈

  • Note

  • Convert string to int
  • Convert int to string
  • You cann't do switch-case statement on string by C++
  • Solution


    No comments:

    Post a Comment