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

[leetcode]Evaluate Reverse Polish Notation-后序表达式计算 java

阅读更多

注意以下几点

1. 原始字符数组输入的顺序,将字符装入list后,需要从第一个字符开始pop,所以代码使用add

2. 使用两个list进行计算

3. 遇到操作符后,运算的两个数字是有序的,代码中为a,b的运算顺序

public class Solution {
    public int evalRPN(String[] tokens) {
         List<String> operations = new ArrayList<String>();
        operations.add("+");
        operations.add("-");
        operations.add("*");
        operations.add("/");
        LinkedList<String> stack = new LinkedList<String>();
        LinkedList<Integer> nums = new LinkedList<Integer>();
        for (String ele : tokens){
            stack.add(ele);
        }
        while (!stack.isEmpty()){
            String ele = stack.pop();
            if(operations.contains(ele)){
                int b = nums.pop();
                int a = nums.pop();
                int result;
                if(ele.equals("+")){
                    result = a+b;
                }else if(ele.equals("-")){
                    result = a-b;
                }else if(ele.equals("*")){
                    result = a*b;
                }else{
                    result = a/b;
                }
                nums.push(result);
            } else {
                nums.push(Integer.parseInt(ele));
            }
        }
        return nums.pop();
        
    }
}

 

分享到:
评论

相关推荐

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

    以逆波兰表示法计算算术表达式的值。 有效的运算符是 +、-、*、/。 每个操作数可以是整数或其他表达式。 笔记: 两个整数之间的除法应向零截断。 给定的 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 ...

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

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

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

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

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

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

    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第一题

    这是evaluate_reverse_polish_notation,这是evaluate_reverse_polish_notat

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

    LeetCode判断字符串是否循环 leetcode Coding with Lin on 1. Minimum Depth of Binary Tree(111) 二叉树的遍历包括: 前序遍历 中序遍历 后序遍历 层次遍历 Points: 采用中序遍历实现 若左右子树均不为空,返回...

    逆波兰表达式求值

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

    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中国-leetcode:刷算法了

    leetcode中国 Leetcode Set Matrix Zeroes 第一种 通过一次遍历记录所有为0的索引(Python中enumerate()输出当前列表的索引) 再遍历一次, 根据记录的索引进行置0 第二种 通过一次遍历所有为0的索引, 设置当前索引的...

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

    最大公共字符串leetcode leetcode刷题打卡 接下来,坚持每周至少刷一道LeetCode题,刷题顺利按照牛客网LeetCode经典题目顺序进行,记录解题思路,与大家共享,同时也做个自我监督 第一周 1、树 题目: Given a ...

    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