leetcode evaluate-reverse-polish-notation
来源:互联网 发布:aspnet源码怎么搭建 编辑:程序博客网 时间:2024/06/08 08:18
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> s = new Stack<>(); int a,b; int c; for(int i =0; i<tokens.length;++i) { if( tokens[i].equals("+")) { b = s.pop(); a = s.pop(); c = a + b; s.push(c); }else if(tokens[i].equals("-")) { b = s.pop(); a = s.pop(); c = a - b; s.push(c); }else if(tokens[i].equals("*")) { b = s.pop(); a = s.pop(); c = a * b; s.push(c); }else if(tokens[i].equals("/")) { b = s.pop(); a = s.pop(); c = a/b; s.push(c); }else { s.push(Integer.parseInt(tokens[i])); } } return s.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
- 2.3 高斯分布
- hive 使用
- assert()函数用法总结
- Python2 爬虫(六) -- 初尝Scrapy框架
- 移动端后台接口约定
- leetcode evaluate-reverse-polish-notation
- WKWebView加载Http页面更新后不刷新问题及解决办法
- hdu 1160 FatMouse's Speed(DP)
- 网络程序设计-为多个客户服务服务器端设计
- HDU 1162.Eddy's picture【最小生成树(Kruskal算法)】【5月30】
- 打印杨辉三角
- 第十、十一周项目1:阅读程序,并写出运行结果(2)
- 51 nod 最高的奖励
- 『数据挖掘』scikit-learn包的进阶学习笔记——第二章:线性回归