zoukankan      html  css  js  c++  java
  • [算法]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. For example:

      ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
      ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
    

    1. Naive Approach

    This problem can be solved by using a stack. We can loop through each element in the given array. When it is a number, push it to the stack. When it is an operator, pop two numbers from the stack, do the calculation, and push back the result.

    public class Test {
    
     
    
    	public static void main(String[] args) throws IOException {
    
    		String[] tokens = new String[] { "2", "1", "+", "3", "*" };
    
    		System.out.println(evalRPN(tokens));
    
    	}
    
     
    
    	public static int evalRPN(String[] tokens) {
    
    		int returnValue = 0;
    
    		String operators = "+-*/";
    
     
    
    		Stack<String> stack = new Stack<String>();
    
     
    
    		for (String t : tokens) {
    
    			if (!operators.contains(t)) { //push to stack if it is a number
    
    				stack.push(t);
    
    			} else {//pop numbers from stack if it is an operator
    
    				int a = Integer.valueOf(stack.pop());
    
    				int b = Integer.valueOf(stack.pop());
    
    				switch (t) {
    
    				case "+":
    
    					stack.push(String.valueOf(a + b));
    
    					break;
    
    				case "-":
    
    					stack.push(String.valueOf(b - a));
    
    					break;
    
    				case "*":
    
    					stack.push(String.valueOf(a * b));
    
    					break;
    
    				case "/":
    
    					stack.push(String.valueOf(b / a));
    
    					break;
    
    				}
    
    			}
    
    		}
    
     
    
    		returnValue = Integer.valueOf(stack.pop());
    
     
    
    		return returnValue;
    
    	}
    
    }

    or

    public class Solution {
    
        public int evalRPN(String[] tokens) {
    
     
    
            int returnValue = 0;
    
     
    
            String operators = "+-*/";
    
     
    
            Stack<String> stack = new Stack<String>();
    
     
    
            for(String t : tokens){
    
                if(!operators.contains(t)){
    
                    stack.push(t);
    
                }else{
    
                    int a = Integer.valueOf(stack.pop());
    
                    int b = Integer.valueOf(stack.pop());
    
                    int index = operators.indexOf(t);
    
                    switch(index){
    
                        case 0:
    
                            stack.push(String.valueOf(a+b));
    
                            break;
    
                        case 1:
    
                            stack.push(String.valueOf(b-a));
    
                            break;
    
                        case 2:
    
                            stack.push(String.valueOf(a*b));
    
                            break;
    
                        case 3:
    
                            stack.push(String.valueOf(b/a));
    
                            break;
    
                    }
    
                }
    
            }
    
     
    
            returnValue = Integer.valueOf(stack.pop());
    
     
    
            return returnValue;
    
     
    
        }
    
    }
  • 相关阅读:
    Highways(prim)
    Help Me with the Game(模拟)
    Parencodings
    The Pilots Brothers' refrigerator
    解决Geany 编辑器无法导入matplotlib包问题
    解决pycharm中导入自定义模块提示出错问题
    解决Pycharm中单元测试未发现问题(No tests were found)
    matplotlib设置中文的的一种方式
    matplotlib入门
    matplotlib入门
  • 原文地址:https://www.cnblogs.com/xiaomoxian/p/5201731.html
Copyright © 2011-2022 走看看