摘要:我们一般看到的数学表达式就是中缀表达式,也就是将符号放在两个数字之间。后缀表达式也就是将运算符放在相应数字的后面。后缀表达式相当于树中的后序遍历。通过获得对应位置的操作符。如果对应的还是操作符,则继续递归往前计算。
题目要求
</>复制代码
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 int evalRPN(String[] tokens) {
LinkedList stack = new LinkedList();
for(String token : tokens){
if(token.equals("+")){
int operand1 = stack.pop();
int operand2 = stack.pop();
stack.push(operand2 + operand1);
}else if(token.equals("-")){
int operand1 = stack.pop();
int operand2 = stack.pop();
stack.push(operand2 - operand1);
}else if(token.equals("*")){
int operand1 = stack.pop();
int operand2 = stack.pop();
stack.push(operand2 * operand1);
}else if(token.equals("/")){
int operand1 = stack.pop();
int operand2 = stack.pop();
stack.push(operand2 / operand1);
}else{
stack.push(Integer.valueOf(token));
}
}
return stack.pop();
}
思路二:递归
从后缀表达式的末尾开始递归获取操作符对应的两个操作符。通过index获得对应位置的操作符。如果对应的还是操作符,则继续递归往前计算。
</>复制代码
int index;
public int evalRPN2(String[] tokens){
index = tokens.length-1;
return recursive(tokens);
}
public int recursive(String[] tokens){
String current = tokens[index--];
int operand1, operand2;
switch(current){
case "+" :
operand1 = recursive(tokens);
operand2 = recursive(tokens);
return operand1 + operand2;
case "-" :
operand1 = recursive(tokens);
operand2 = recursive(tokens);
return operand2 - operand1;
case "*" :
operand1 = recursive(tokens);
operand2 = recursive(tokens);
return operand2 * operand1;
case "/" :
operand1 = recursive(tokens);
operand2 = recursive(tokens);
return operand2 / operand1;
default:
return Integer.valueOf(current);
}
}
想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/70232.html
Problem Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Note: Division between two inte...
摘要:题目链接来做,保存数字,碰到符号的时候就弹出两个数字计算算完后再放入,最后里面的就是结果。 150. Evaluate Reverse Polish Notation 题目链接:https://leetcode.com/problems... stack来做,保存数字,碰到符号的时候就弹出两个数字计算算完后再放入stack,最后stack里面的就是结果。 public class So...
摘要:题目根据逆波兰表示法,求表达式的值。给定逆波兰表达式总是有效的。逆波兰表达式又叫做后缀表达式。解题思路可以看出逆波兰表达式中的每一个运算符属于该运算符前的两个数字间的运算。如如波兰表达式则加号前两个数字为。 题目: 根据逆波兰表示法,求表达式的值。 有效的运算符包括 +, -, *, / 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。 Evaluate the value of...
摘要:小鹿题目根据逆波兰表示法,求表达式的值。给定逆波兰表达式总是有效的。算法思路仔细观察上述的逆波兰表达式,可以发现一个规律就是每遇到一个操作符,就将操作符前的两个操作数进行运算,将结果保存到原位置。 Time:2019/4/14Title: Evaluate Reverse Polish NotationDifficulty: MediumAuthor:小鹿 题目:Evaluate ...
摘要:栈法复杂度时间空间思路逆波兰表达式的计算十分方便,对于运算符,其运算的两个数就是这个运算符前面的两个数。注意对于减法,先弹出的是减号后面的数。 Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operato...
阅读 3477·2023-04-25 16:25
阅读 4057·2021-11-15 18:01
阅读 1767·2021-09-10 11:21
阅读 3269·2021-08-02 16:53
阅读 3230·2019-08-30 15:55
阅读 2630·2019-08-29 16:24
阅读 2255·2019-08-29 13:14
阅读 1283·2019-08-29 13:00