leetcode--002 rpn
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的更多相关文章
- LeetCode #002# Add Two Numbers(js描述)
索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...
- Leetcode 002. 两数相加
1.题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表 ...
- [Leetcode] 002. Add Two Numbers
https://leetcode.com/problems/add-two-numbers/ public class Solution { public ListNode addTwoNumbers ...
- 【JAVA、C++】LeetCode 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- Add Two Numbers ---- LeetCode 002
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- leetcode刷题: 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- leetcode python 002
##002 Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8# 链表节点都是一位数字,以上可以视为2 ...
- 【LeetCode刷题系列 - 002题】Add Two Numbers
题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- 【LeetCode】002 Add Two Numbers
题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- LeetCode题解002:两数相加
两数相加 题目 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字 如果,我们将这两个数相加起来,则会返回一个新的链表 ...
随机推荐
- elasticsearch 索引优化
ES索引优化篇主要从两个方面解决问题,一是索引数据过程:二是检索过程. 索引数据过程我在上面几篇文章中有提到怎么创建索引和导入数据,但是大家可能会遇到索引数据比较慢的过程.其实明白索引的原理就可以有 ...
- J2SE基本数据结构
1.J2SE中的常用数据结构对象的继承关系如下图 Collection ........|--------List ........|..........|----------ArrayList .. ...
- POJ2096 概率dp 入门
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=118282#problem/B 挺好的一个题目: 不过刚开始嘛,看别人题解长知识.这个人写 ...
- android - 自定义(组合)控件 + 自定义控件外观
转载:http://www.cnblogs.com/bill-joy/archive/2012/04/26/2471831.html android - 自定义(组合)控件 + 自定义控件外观 A ...
- 关于Tcpreplay
tcpprep -p -o /root/Desktop/ZS/Tcpreplay/cache_test.cache -i /root/Desktop/ZS/Tcpreplay/9.17.pcap tc ...
- over-float清除浮动
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- PAT (Advanced Level) 1062. Talent and Virtue (25)
简单排序.题意较长. #include<cstdio> #include<cstring> #include<cmath> #include<queue> ...
- @Resource @Autowired 区别
spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖.在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入 ...
- AIDL原理解析
首先为什么需要aidl? 下面是不需要aidl 的binder的IPC通讯过程,表面上结构很简单,但是有个困难就是,客户端和服务端进行通讯,你得先将你的通讯请求转换成序列化的数据,然后调用transa ...
- 合并BIN文件的两种方法(转)
源:http://blog.chinaunix.net/uid-20745340-id-1878803.html 合并BIN文件的两种方法 在单片机的开发过程中,经常需要将两个单独的BIN文件合并成一 ...