package leetcode;

import java.util.HashSet;
import java.util.Stack;

public class demo_150 {
    public int evalRPN(String[] tokens) {
        HashSet<String> hs=new HashSet<String>();
        hs.add("*");
        hs.add("+");
        hs.add("-");
        hs.add("/");
        Stack<String> stack=new Stack<String>();
        //将数组中的所有数据存放到栈中
        for(String s:tokens) {
            //如果是运算符取出栈中的前两个数字进行运算后在存入栈中
            if(hs.contains(s)) {
                int a=Integer.valueOf(stack.pop());
                int b=Integer.valueOf(stack.pop());
                int c=0;
                if(s.equals("*")) {
                    c=b*a;
                }
                if(s.equals("+")) {
                    c=b+a;
                }
                if(s.equals("/")) {
                    c=b/a;
                }
                if(s.equals("-")) {
                    c=b-a;
                }
                stack.push(String.valueOf(c));
            }
            //数字直接放入栈中
            else {
                stack.push(s);
            }
        }
        System.out.println(stack.peek());
        return Integer.valueOf(stack.peek());
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        demo_150 d150=new demo_150();
        String[] tokens= {"10","6","9","3","+","-11","*","/","*","17","+","5","+"};
        d150.evalRPN(tokens);
    }
}