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

來源:互聯網
上載者:User

標籤:token   another   break   exp   逆波蘭運算式   code   []   tor   http   

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();    }        }

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.