package leetcode;

 import java.util.Stack;

 public class RPN {
public static int evalRPN(String[] tokens) {
Stack stack=new Stack();
for(int i=0;i<tokens.length;i++){
if(tokens[i]=="+"){
int a=Integer.parseInt((String)stack.pop());
int b=Integer.parseInt((String)stack.pop());
stack.push(a+b);
}else if(tokens[i]=="-"){
int a=Integer.parseInt((String)stack.pop());
int b=Integer.parseInt((String)stack.pop());
stack.push(a-b);
}else if(tokens[i]=="*"){
int a=Integer.parseInt((String)stack.pop());
int b=Integer.parseInt((String)stack.pop());
stack.push(a*b);
}else if(tokens[i]=="/"){
int a=Integer.parseInt((String)stack.pop());
int b=Integer.parseInt((String)stack.pop());
if(b==0){
return 0; }else{
stack.push(a/b);
} }else{
stack.push(tokens[i]);
} }
int result=(int)stack.peek();
return result;
}
public static void main(String[] args){
String[] tokens={"0","3","/"}; System.out.println(evalRPN(tokens));
}
}

leetcode--002 rpn的更多相关文章

  1. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  2. Leetcode 002. 两数相加

    1.题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表 ...

  3. [Leetcode] 002. Add Two Numbers

    https://leetcode.com/problems/add-two-numbers/ public class Solution { public ListNode addTwoNumbers ...

  4. 【JAVA、C++】LeetCode 002 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  5. Add Two Numbers ---- LeetCode 002

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  6. leetcode刷题: 002 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  7. leetcode python 002

    ##002 Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8# 链表节点都是一位数字,以上可以视为2 ...

  8. 【LeetCode刷题系列 - 002题】Add Two Numbers

    题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  9. 【LeetCode】002 Add Two Numbers

    题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  10. LeetCode题解002:两数相加

    两数相加 题目 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字 如果,我们将这两个数相加起来,则会返回一个新的链表 ...

随机推荐

  1. lucene索引合并与增量索引

    利用 Lucene,在创建索引的工程中你可以充分利用机器的硬件资源来提高索引的效率.当你需要索引大量的文件时,你会注意到索引过程的瓶颈是在往磁盘上写索引文件的过程中.为了解决这个问题, Lucene ...

  2. Python -- OOP高级 -- __slots__、@property

    __slots__属性可以设置 允许被设置的属性 class Student: __slots__ = ("name", "age") >>> ...

  3. 多校 Cow Bowling

    题目链接:http://acm.hust.edu.cn/vjudge/contest/124435#problem/I 密码:acm Sample Input Sample Output 分析: #i ...

  4. s5pv210 AD转换

    1:ADC:Analog-to-Digital Converter,模拟信号转数字信号,自然界一般为模拟信号,而SoC需要数字信号,所以之间通信需要ADC. 2:转换原理: 以逐次逼近式AD转换为例: ...

  5. MySQL+heartbeat+nfs做高可用

    一.环境准备节点两个node1:10.10.10.202node2:10.10.10.203nfs服务器:node3:10.10.10.204系统环境CentOS release 6.5 (Final ...

  6. Review Board的使用

    代码审核工具.先在命令行界面,进入到工程的Main目录下,然后使用命令 svn diff>yus.diff  这样就将Main里面的所有内容生成了,然后在浏览器里进入到自己的Review Boa ...

  7. linux export将PATH环境变量误删了的解决办法

    今天新增环境变量的时候不小心把冒号错打成了分号 export PATH=/usr/local/php5/bin;$PATH; 导致PATH变量为/usr/local/php/bin 解决办法:[ubu ...

  8. javascript event bubbling and capturing (再谈一谈js的事件冒泡和事件补获,看到这篇文章加深了理解)

    原文地址:http://javascript.info/tutorial/bubbling-and-capturing 先给出最终的结论: Summary Events first are captu ...

  9. java.net.URLEncode编码 与 URLDecode解码问题

    1.java内部加密和解密 String mytext = java.net.URLEncoder.encode("中国", "utf-8"); String ...

  10. jQuery+CSS实现的图片滚动效果

    http://www.helloweba.com/view-blog-139.html