leetcode--evaluate-reverse-polish-notation
来源:互联网 发布:淘宝网卖家体检中心 编辑:程序博客网 时间:2024/06/01 09:01
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.*;public class Solution { public int evalRPN(String[] tokens) { Stack<Integer> stackNum=new Stack<>(); int len=tokens.length; for(int i=0;i<len;i++){ try{ stackNum.push(Integer.parseInt(tokens[i])); }catch(Exception e){ String operation=tokens[i]; int numA=stackNum.pop(); int numB=stackNum.pop(); switch(operation){ case "+": stackNum.push(numB+numA); break; case "-": stackNum.push(numB-numA); break; case "*": stackNum.push(numB*numA); break; case "/": stackNum.push(numB/numA); break; } } } return stackNum.pop(); }}
阅读全文
0 0
- 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
- 【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
- java.lang.OutOfMemoryError: Java heap space解决方法
- angularjs数据请求、分页问题
- 在VC2015里包含了lib库,但没有设置对路径的出错
- RGB565转换为RGB88,并作线性补偿
- HDU 4704 Sum(费马小定理,组合数学,快速幂)
- leetcode--evaluate-reverse-polish-notation
- 我的C++学习之路
- 多线程问题总结
- Oracle 导出导入数据
- 四、组件化和属性(props)
- IOS IJKPlayer播放RTSP
- Xshell连接Linux
- Android 7.0 自定义系统service过程与注意事项
- php底层分析