`
frank-liu
  • 浏览: 1666858 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

leetcode: Evaluate Reverse Polish Notation

 
阅读更多

问题描述:

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

原问题链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/

 

问题分析

  关于逆波兰表达式的计算,其实是有固定的套路的。它和我们平常的表达式计算不一样,我们一般表达式里,计算符是放在两个操作数的中间,而这里计算符是放在两个操作数的后面。但是这个问题的解决思路还是比较简单的,我们每次碰到数字的时候就将它压入栈中,碰到计算符号时就将栈顶的两个元素弹出来参与计算,并将计算后的结果放到栈里面。

  按照这个思路,可以很容易得到如下的代码:

 

public class Solution {
    public int evalRPN(String[] tokens) {
        LinkedList<Integer> stack = new LinkedList<>();
        for(String str : tokens) {
            if(str.equals("+")) {
                stack.push(stack.pop() + stack.pop());
            } else if(str.equals("-")) {
                int second = stack.pop();
                stack.push(stack.pop() - second);
            } else if(str.equals("*")) {
                int result = stack.pop() * stack.pop();
                stack.push(result);
            } else if(str.equals("/")) {
                int second = stack.pop();
                stack.push(stack.pop() / second);
            } else {
                stack.push(Integer.parseInt(str));
            }
        }
        return stack.peek();
    }
}

 

1
8
分享到:
评论

相关推荐

    最大公共字符串leetcode-leetcode:坚持每周刷一道LeetCode题

    最大公共字符串leetcode leetcode刷题打卡 接下来,坚持每周至少刷一道LeetCode题,...Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are+,-,*,/. Each operand may b

    javalruleetcode-leetcode:这是Java写的leetcode

    [Java](./src/Evaluate Reverse Polish Notation/Solution.java) 2013/11/27 中等的 [Java](./src/直线上的最大点数/Solution.java) 2013/11/22 难的 [Java](./src/排序列表/Solution.java) 2013/11/16 中等的 [Java...

    leetcode中国-leetcode:刷算法了

    Evaluate Reverse Polish Notation 逆波兰的后半截实现 Linked List Cycle 快慢指针 Linked List Cycle II 快慢指针再加个初始指针 慢指针到链表开始位置时, 快指针总是比他快k步(每次移动快1步, 移动k次), 第一次...

    LeetCode判断字符串是否循环-leetcode:用lin编码

    Notation(150) Vector 向量 vector 是一种对象实体, 能够容纳许多其他类型相同的元素, 因此又被称为容器。 与string相同, vector 同属于STL(Standard Template Library, 标准模板库)中的一种自定义的数据类型, ...

    LeetCode:LeetCode解决方案

    LeetCodeLeetCode solutions(Java)树Minimum Depth of Binary Tree栈evaluate-reverse-polish-notation穷举max-points-on-a-line链表sort-list排序insertion-sort-list树binary-tree-postorder-traversal树binary-...

    leetcode不会-evaluate-reverse-polish-notation:评估反向波兰语表达

    leetcode 不会评估反向波兰表示法 以逆波兰表示法计算算术表达式的值。 有效的运算符是 +、-、*、/。 每个操作数可以是整数或其他表达式。 笔记: 两个整数之间的除法应向零截断。 给定的 RPN 表达式始终有效。 这...

    Leetcode的ac是什么意思-LeetCodeInJava:leetcode-java

    Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate ...

    Coding Interview In Java

    3 Evaluate Reverse Polish Notation 21 4 Isomorphic Strings 25 5 Word Ladder 27 6 Word Ladder II 29 7 Median of Two Sorted Arrays 33 8 Kth Largest Element in an Array 35 9 Wildcard Matching 37 10 ...

    leetcode第一题

    这是evaluate_reverse_polish_notation,这是evaluate_reverse_polish_notat

    leetcode分发糖果-ForDaFactory:使用C++的个人leetcode解决方案

    leetcode分发糖果 ...150-逆波兰表达式求值:evaluate-reverse-polish-notation 155-最小栈:min-stack Tree 普通二叉树 94-二叉树中序遍历:inorder-traversal 100-相同的二叉树:same-tree 101-对称二叉树:symmetric-

    leetcode双人赛-Algorithm:leetcode算法刷题总结,长期更新,欢迎探讨与指正

    [leetcode]150.Evaluate Reverse Polish Notation 题目: ["2", "1", "+", "3", "*"] -&gt; ((2 + 1) * 3) -&gt; 9 ["4", "13", "5", "/", "+"] -&gt; (4 + (13 / 5)) -&gt; 6 思路:每次遇到符号,出栈两个元素进行运算,并将...

    LeetCode最全代码

    190 | [Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [C++](./C++/reverse-bits.cpp) [Python](./Python/reverse-bits.py) | _O(1)_ | _O(1)_ | Easy ||| 191 |[Number of 1 Bits]...

    gasstationleetcode-leetcode-in-niuke:在牛客网上的在线编程中的leetcode在线编程题解

    evaluate-reverse-polish-notation 链表 sort-list 排序 insertion-sort-list 树 binary-tree-postorder-traversal 树 binary-tree-preorder-traversal 链表 linked-list-cycle-ii 链表 linked-list-cycle 链表 copy...

    逆波兰表达式求值

    链接:https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/ 思路 运用lambda表达式和字典对运算符进行重载,再通过栈 stacks 对运算对象及中间结果进行存储 Python代码 class Solution: def ...

    cpp-算法精粹

    Evaluate Reverse Polish Notation Implement Stack using Queues 队列 Implement Queue using Stacks 二叉树 二叉树的遍历 Binary Tree Preorder Traversal Binary Tree Inorder Traversal Binary Tree Postorder ...

Global site tag (gtag.js) - Google Analytics