LeetCode Reverse Polish Notation求逆波兰表达式值

public class Solution {
public static int evalRPN(String[] tokens)
	{
		Stack<String> stack = new Stack<String>();
		String operatorsString = "+-*/";
		for(int i = 0 ; i < tokens.length ; i++)
		{
			if(!operatorsString.contains(tokens[i]))
				stack.push(tokens[i]);
			else {
				int result = 0;
				int value1 = Integer.valueOf(stack.pop());
				int value2 = Integer.valueOf(stack.pop());
				int index = operatorsString.indexOf(tokens[i]);
				switch (index) {
				case 0:
					result = value1 + value2;
					break;
				case 1:
					result = value2 - value1;
					break;
				case 2:
					result = value1 * value2;
					break;
				case 3:
					result = value2 / value1;
				}
				stack.push(String.valueOf(result));
			}
		}
		
		return Integer.valueOf(stack.pop());
	}
}

你可能感兴趣的:(LeetCode Reverse Polish Notation求逆波兰表达式值)