LeetCode 2 Evaluate Reverse Polish Notation
来源:互联网 发布:剑三高冷成男捏脸数据 编辑:程序博客网 时间:2024/06/05 00:53
Evaluate the value of arithmetic expression in Reverse Polish Notation.
Valid operator 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 class Solution { public int evalRPN(String[] tokens) { Stack<Integer> st = new Stack<Integer>(); for(String token : tokens){ if(token.matches("-?[0-9]+")){ st.push(Integer.parseInt(token)); }else{ int num2 = st.pop(); int num1 = st.pop(); if(token.equals("+")){ st.push(num1+num2); }else if(token.equals("-")){ st.push(num1-num2); }else if(token.equals("*")){ st.push(num1*num2); }else if(token.equals("/")){ st.push(num1/num2); } } } return st.pop(); }}
0 0
- LeetCode problem 2: Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation [2]
- LeetCode 2 Evaluate Reverse Polish Notation
- LeetCode 2 Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation【leetcode 2】
- Evaluate Reverse Polish Notation | leetcode
- leetcode-Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- LeetCode - Evaluate Reverse Polish Notation
- LeetCode | Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation (LeetCode)
- leetcode: Evaluate Reverse Polish Notation
- Leetcode Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation -- LeetCode
- 自定义简单的TableView
- AlertDialog对话框
- MTK平台LCD驱动框架详解(二)
- 找新朋友(杭电1286)
- 直方图匹配方法
- LeetCode 2 Evaluate Reverse Polish Notation
- 腾讯后台开发三面面试题
- Android的LogCat的使用
- poj 3254
- JTable添加一行
- Search in Rotated Sorted Array
- Tui-x 自适应屏幕
- hdu 3342 拓扑排序
- 使用WPF内置的路由事件