• leetcode 150. Evaluate Reverse Polish Notation ------ java


    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
    

     就是求逆波兰表达式(后续遍历)的结果。

    1、直接求解,很慢

    public class Solution {
        public int evalRPN(String[] tokens) {
            
            int len = tokens.length;
            if( len == 0)
                return 0;
            for( int i = 0 ; i < len ; i ++ ){
                
                if( tokens[i].equals("+") )
                    helper(tokens,i,1);
                else if( tokens[i].equals("-") )
                    helper(tokens,i,2);
                else if( tokens[i].equals("*") )
                    helper(tokens,i,3);
                else if( tokens[i].equals("/") )
                    helper(tokens,i,4);
            }
            return Integer.valueOf(tokens[0]);
        }
        public void helper(String[] tokens,int pos,int type){
            int pos1 = -1,pos2 = 0 ;
            tokens[pos] = null;
            while( pos >= 0 ){
                if( tokens[pos] != null){
                    if( pos1 == -1)
                        pos1 = pos;
                    else{
                        pos2 = pos;
                        break;
                    }
                }
                pos--;
            }
            int num1 = Integer.valueOf(tokens[pos1]);
            int num2 = Integer.valueOf(tokens[pos2]);
            if( type == 1){
                tokens[pos2] = String.valueOf(num1+num2);
            }else if( type == 2){
                tokens[pos2] = String.valueOf(num2-num1);
            }else if( type == 3){
                tokens[pos2] = String.valueOf(num2*num1);
            }else if( type == 4){
                tokens[pos2] = String.valueOf(num2/num1);
            }
            tokens[pos1] = null;
        } 
        
    }

    2、使用栈,很简单。

    public class Solution {
        public int evalRPN(String[] tokens) {
            
            int len = tokens.length;
            
            if( len == 0)
                return 0;
            Stack<Integer> stack = new Stack<Integer>();
            for( int i = 0 ; i < len ; i ++ ){
                
                if( tokens[i].equals("+") ){
                    int num1 = stack.pop();
                    int num2 = stack.pop();
                    stack.push(num1+num2);
                }
                else if( tokens[i].equals("-") ){
                    int num1 = stack.pop();
                    int num2 = stack.pop();
                    stack.push(num2-num1);
                }
                else if( tokens[i].equals("*") ){
                    int num1 = stack.pop();
                    int num2 = stack.pop();
                    stack.push(num1*num2);
                }
                else if( tokens[i].equals("/") ){
                    int num1 = stack.pop();
                    int num2 = stack.pop();
                    stack.push(num2/+num1);
                    
                }else{
                    stack.push(Integer.valueOf(tokens[i]));
                }
    
            }
            return stack.pop();
        }
        
        
    }
  • 相关阅读:
    hdu5587 BestCoder Round #64 (div.2)
    hdu5569 BestCoder Round #63 (div.2)
    hihocoder1257(构造)(2015北京ACM/ICPC)
    hihocoder 1249(2015ACM/ICPC北京)
    hihocoder1258(水)(2015ACM/ICPC北京站)
    hihiocoder 1255(搜索)(2015ACM/ICPC北京站)
    习题9-8 uva1631
    习题9-8 Uva1632
    Orz
    习题9-6 uva 10723
  • 原文地址:https://www.cnblogs.com/xiaoba1203/p/6078708.html
Copyright © 2020-2023  润新知