博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Evaluate Reverse Polish Notation
阅读量:4350 次
发布时间:2019-06-07

本文共 1104 字,大约阅读时间需要 3 分钟。

Evaluate the value of an arithmetic expression in .

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

 

class Solution {    public int evalRPN(String[] tokens) {        Stack
stack =new Stack<>(); for(String s:tokens){ if(s.equals("+")){ int a=stack.pop(); int b=stack.pop(); stack.push(a+b); }else if(s.equals("-")){ int a=stack.pop(); int b=stack.pop(); stack.push(b-a); }else if(s.equals("*")){ int a=stack.pop(); int b=stack.pop(); stack.push(a*b); }else if(s.equals("/")){ int a=stack.pop(); int b=stack.pop(); stack.push(b/a); }else{ stack.push(Integer.parseInt(s)); } } return stack.pop(); }}

 

转载于:https://www.cnblogs.com/xiaolovewei/p/8274499.html

你可能感兴趣的文章
笔试编程---快手实习题目
查看>>
csp20170304地铁修建_Solution
查看>>
快速沃尔什变换 与 快速莫比乌斯变换
查看>>
SQL的四种连接-左外连接、右外连接、内连接、全连接
查看>>
Palindromic Substrings
查看>>
改变和恢复view的方向
查看>>
C#调用金数据API
查看>>
用事实说话,成熟的ORM性能不是瓶颈,灵活性不是问题:EF5.0、PDF.NET5.0、Dapper原理分析与测试手记(转)...
查看>>
Convert Sorted List to Binary Search Tree
查看>>
Leetcode:Unique Binary Search Trees
查看>>
D3.js 绘制散点图
查看>>
《图解HTTP》
查看>>
python之路_面向对象
查看>>
CSS
查看>>
jvm架构以及Tomcat优化
查看>>
数据库の目录
查看>>
vmware安装rhel 7
查看>>
[复合材料] 编织复合材料单胞周期性边界条件编程问题
查看>>
Paxos协议笔记
查看>>
php之快速入门学习-15(php函数)
查看>>