面试的时候遇到的一道题,输入逆波兰表达式,计算并输出结果。当时没写完整,事后重新写了一遍,并加了一个将普通表达式转换成逆波兰表达式的方法。
这是面试题:
/**
* 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
*/
public class EvalRPN{
public static void test() {
Assert.assertTrue(9 == evalRPN(new String[] { "2", "1", "+", "3", "*" }));
Assert.assertTrue(6 == evalRPN(new String[] { "4", "13", "5", "/", "+" }));
}
// complete this method
public static int evalRPN(String[] tokens) {
int returnValue = 0;
// Your code here (about 30 lines)
return returnValue;
}
}
完整代码如下,没有考虑多位数运算,非0判断,小数等问题。
/**
* 利用逆波兰表达式,计算中缀表达式
*/
public class EvalRPN{
/**
* 计算逆波兰表达式
* 1. 当读到一个数字时就将它压入栈中
* 2. 读到一个运算符时,就从栈中弹出两个数字,并将该运算符作用于这两个数字,然后将计算结果再压入栈中
* @param ArrayList存储的逆波兰表达式的
* @return 计算结果
*/
public static int evalRPN(ArrayList<String> tokens) {
int returnValue = 0;
Stack<String> temp = new Stack<String>();
for (int n = 0; n < tokens.size(); n++) {
int i;
if (tokens.get(n).equals("+")) {
i = Integer.parseInt(temp.get(temp.size()-1)) + Integer.parseInt(temp.get(temp.size()-2));
temp.pop();
temp.pop();
temp.push(i+"");
} else if (tokens.get(n).equals("-")) {
i = Integer.parseInt(temp.get(temp.size()-1)) - Integer.parseInt(temp.get(temp.size()-2));
temp.pop();
temp.pop();
temp.push(i+"");
} else if (tokens.get(n).equals("*")) {
i = Integer.parseInt(temp.get(temp.size()-1)) * Integer.parseInt(temp.get(temp.size()-2));
temp.pop();
temp.pop();
temp.push(i+"");
} else if (tokens.get(n).equals("/")) {
i = Integer.parseInt(temp.get(temp.size()-2)) / Integer.parseInt(temp.get(temp.size()-1));
temp.pop();
temp.pop();
temp.push(i+"");
} else {
temp.push(tokens.get(n));
}
}
returnValue = Integer.parseInt(temp.peek());
return returnValue;
}
/**
* 将中缀表达式转换为后缀表达式(逆波兰表达式)
* 1.读到的是操作数,直接输出;
* 2.读到的是操作符(记为read),将其与栈顶的操作符(记为top)进行优先级比较:read>top,read入栈,继续读下一个;
* read≤top,top出栈,并输出到list中,read继续和新的top比较;top为空,read直接入栈;若top是“(”,read直接入栈,因为“(”优先级最高;
* 3.括号的处理:读到左括号“(”,直接将其压入栈中,并且除非遇到右括号“)”,“(”是不会弹出的;读到右括号“)”,将“(”之上的元素全部依次输出,并弹出
* “(”,但不输出;
*
* @param expression 用户输入的中缀表达式
* @return 转化的后缀表达式
*/
public static ArrayList<String> ReversePN(String expression){
ArrayList<String> list = new ArrayList<String>();
Stack<String> stack = new Stack<String>();
Map<String, Integer> priority = new HashMap<String, Integer>();
priority.put("+", 1);
priority.put("-", 1);
priority.put("*", 2);
priority.put("/", 2);
priority.put("(", 0);
for (int i = 0; i < expression.length(); i++) {
String read = String.valueOf(expression.charAt(i));
if (!" ".equals(read)
&& ("+".equals(read) || "-".equals(read)
|| "*".equals(read) || "/".equals(read)
|| "(".equals(read) || ")".equals(read))) {
if (stack.empty() || read.equals("(")) {
stack.push(read);
} else if (read.equals(")")) {
int j = stack.search("(");
for (int j2 = 1; j2 < j; j2++) {
list.add(stack.pop());
}
stack.pop();
} else if (priority.get(read) > priority.get(stack.peek())) {
stack.push(read);
} else {
list.add(stack.pop());
stack.push(read);
}
} else if (!" ".equals(read)) {
list.add(read);
}
}
for (String string : stack) {
list.add(string);
}
return list;
}
}
参考文章: