【Leetcode-medium-150】Evaluate Reverse Polish Notation
来源:互联网 发布:考研网络培训班 编辑:程序博客网 时间:2024/05/29 05:55
【Leetcode-medium-150】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
思路
栈
程序
import java.util.*;class Solution { public int evalRPN(String[] tokens) { if (tokens == null || tokens.length == 0) throw new RuntimeException("illegal argument exception."); LinkedList<Integer> stack = new LinkedList<>(); HashSet<String> set = new HashSet<>(); set.add("+"); set.add("-"); set.add("*"); set.add("/"); for (String token : tokens){ if (set.contains(token)){ int num2 = Integer.valueOf(stack.pop()); int num1 = Integer.valueOf(stack.pop()); switch(token){ case "+" : stack.push(num1 + num2); break; case "-" : stack.push(num1 - num2); break; case "*" : stack.push(num1 * num2); break; case "/" : stack.push(num1 / num2); break; } }else{ stack.push(Integer.valueOf(token)); } } return stack.peek(); }}
阅读全文
1 0
- [Leetcode 150, Medium] Evaluate Reverse Polish Notation
- 【Leetcode-medium-150】Evaluate Reverse Polish Notation
- Leetcode 150. Evaluate Reverse Polish Notation (Medium) (cpp)
- LeetCode 150 Evaluate Reverse Polish Notation
- LeetCode: Evaluate Reverse Polish Notation [150]
- LeetCode(150) Evaluate Reverse Polish Notation
- [leetcode 150] Evaluate Reverse Polish Notation
- [LeetCode 150]Evaluate Reverse Polish Notation
- Leetcode 150:Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation - LeetCode 150
- LeetCode---(150)Evaluate Reverse Polish Notation
- leetcode[150]:Evaluate Reverse Polish Notation
- LeetCode(150) Evaluate Reverse Polish Notation
- [leetcode-150]Evaluate Reverse Polish Notation(c++)
- leetcode 150: Evaluate Reverse Polish Notation
- 【leetcode】No.150 Evaluate Reverse Polish Notation
- LeetCode(150) Evaluate Reverse Polish Notation
- LeetCode 150 Evaluate Reverse Polish Notation
- JavaSE知识点汇总
- 【计算机网络 】wireshark笔记
- oj网站的训练题:多个数求和
- 数据源配置
- Effective Java 读书笔记(三):类和接口
- 【Leetcode-medium-150】Evaluate Reverse Polish Notation
- 设计模式---外观模式
- Opsive 的Third Person Controller使用手记(1)
- PAT乙级1029. 旧键盘(20)
- javascript执行上下文、作用域与闭包(第四篇)---作用域与执行上下文
- vijos1843(货车运输)
- Python实现RGB与HSI颜色空间的互换
- Lintcode144 Interleaving Positive And Negative Numbers solution 题解
- C++学习摘录(类型和声明)