资讯专栏INFORMATION COLUMN

leetcode150. Evaluate Reverse Polish Notation

bitkylin / 3432人阅读

摘要:我们一般看到的数学表达式就是中缀表达式,也就是将符号放在两个数字之间。后缀表达式也就是将运算符放在相应数字的后面。后缀表达式相当于树中的后序遍历。通过获得对应位置的操作符。如果对应的还是操作符,则继续递归往前计算。

题目要求

</>复制代码

  1. Evaluate the value of an arithmetic expression in Reverse Polish Notation.
  2. Valid operators are +, -, *, /. Each operand may be an integer or another expression.
  3. Some examples:
  4. ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
  5. ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6

计算后缀表达式。我们一般看到的数学表达式就是中缀表达式,也就是将符号放在两个数字之间。后缀表达式也就是将运算符放在相应数字的后面。后缀表达式相当于树中的后序遍历。

思路一:栈

当我们遇到数字时就将数字压入栈中,如果遇到操作符就将栈顶的两个数字弹出,并将其根据操作符计算结构并重新压入栈中。栈中剩下的最后的值就是我们的结果。

</>复制代码

  1. public int evalRPN(String[] tokens) {
  2. LinkedList stack = new LinkedList();
  3. for(String token : tokens){
  4. if(token.equals("+")){
  5. int operand1 = stack.pop();
  6. int operand2 = stack.pop();
  7. stack.push(operand2 + operand1);
  8. }else if(token.equals("-")){
  9. int operand1 = stack.pop();
  10. int operand2 = stack.pop();
  11. stack.push(operand2 - operand1);
  12. }else if(token.equals("*")){
  13. int operand1 = stack.pop();
  14. int operand2 = stack.pop();
  15. stack.push(operand2 * operand1);
  16. }else if(token.equals("/")){
  17. int operand1 = stack.pop();
  18. int operand2 = stack.pop();
  19. stack.push(operand2 / operand1);
  20. }else{
  21. stack.push(Integer.valueOf(token));
  22. }
  23. }
  24. return stack.pop();
  25. }
思路二:递归

从后缀表达式的末尾开始递归获取操作符对应的两个操作符。通过index获得对应位置的操作符。如果对应的还是操作符,则继续递归往前计算。

</>复制代码

  1. int index;
  2. public int evalRPN2(String[] tokens){
  3. index = tokens.length-1;
  4. return recursive(tokens);
  5. }
  6. public int recursive(String[] tokens){
  7. String current = tokens[index--];
  8. int operand1, operand2;
  9. switch(current){
  10. case "+" :
  11. operand1 = recursive(tokens);
  12. operand2 = recursive(tokens);
  13. return operand1 + operand2;
  14. case "-" :
  15. operand1 = recursive(tokens);
  16. operand2 = recursive(tokens);
  17. return operand2 - operand1;
  18. case "*" :
  19. operand1 = recursive(tokens);
  20. operand2 = recursive(tokens);
  21. return operand2 * operand1;
  22. case "/" :
  23. operand1 = recursive(tokens);
  24. operand2 = recursive(tokens);
  25. return operand2 / operand1;
  26. default:
  27. return Integer.valueOf(current);
  28. }
  29. }


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70232.html

相关文章

  • [LeetCode] 150. Evaluate Reverse Polish Notation

    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...

    KoreyLee 评论0 收藏0
  • 150. Evaluate Reverse Polish Notation

    摘要:题目链接来做,保存数字,碰到符号的时候就弹出两个数字计算算完后再放入,最后里面的就是结果。 150. Evaluate Reverse Polish Notation 题目链接:https://leetcode.com/problems... stack来做,保存数字,碰到符号的时候就弹出两个数字计算算完后再放入stack,最后stack里面的就是结果。 public class So...

    yanbingyun1990 评论0 收藏0
  • LeetCode 150:逆波兰表达式求值 Evaluate Reverse Polish Nota

    摘要:题目根据逆波兰表示法,求表达式的值。给定逆波兰表达式总是有效的。逆波兰表达式又叫做后缀表达式。解题思路可以看出逆波兰表达式中的每一个运算符属于该运算符前的两个数字间的运算。如如波兰表达式则加号前两个数字为。 题目: 根据逆波兰表示法,求表达式的值。 有效的运算符包括 +, -, *, / 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。 Evaluate the value of...

    Turbo 评论0 收藏0
  • LeetCode 之 JavaScript 解答第150题 —— 逆波兰表达式求值

    摘要:小鹿题目根据逆波兰表示法,求表达式的值。给定逆波兰表达式总是有效的。算法思路仔细观察上述的逆波兰表达式,可以发现一个规律就是每遇到一个操作符,就将操作符前的两个操作数进行运算,将结果保存到原位置。 Time:2019/4/14Title: Evaluate Reverse Polish NotationDifficulty: MediumAuthor:小鹿 题目:Evaluate ...

    104828720 评论0 收藏0
  • [Leetcode] Evaluate Reverse Polish Notation 计算逆波兰表

    摘要:栈法复杂度时间空间思路逆波兰表达式的计算十分方便,对于运算符,其运算的两个数就是这个运算符前面的两个数。注意对于减法,先弹出的是减号后面的数。 Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operato...

    ephererid 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<