Phone Interview Dec 2017, Reverse Polish Notation


  • 0
    J
    /** Compute the value of an expression in Reverse Polish Notation. Supported operators are "+", "-", "*" and "/".
     * Reverse Polish is a postfix mathematical notation in which each operator immediately follows its operands.
     * Each operand may be a number or another expression.
     * For example, 3 + 4 in Reverse Polish is 3 4 + and 2 * (4 + 1) would be written as 4 1 + 2 * or 2 4 1 + *
     *
     * @param ops a sequence of numbers and operators, in Reverse Polish Notation
     * @return the result of the computation
     * @throws IllegalArgumentException ops don't represent a well-formed RPN expression
     * @throws ArithmeticException the computation generates an arithmetic error, such as dividing by zero
     *
     * Some sample ops and their results:
     * ["4", "1", "+", "2.5", "*"] -> ((4 + 1) * 2.5) -> 12.5
     * ["5", "80", "40", "/", "+"] -> (5 + (80 / 40)) ->  7
     * ["1"] -> 1
     * ["1", "2"] -> not valid
     */
    public double rpn (List<String> ops) throws IllegalArgumentException, ArithmeticException {
    }
    

  • 0
    J

    I solved this problem during the interview.
    Has no one who have visited this problem attempted to solve it?
    I was hoping to have an open discussion regarding alternative solutions if any.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.