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:两数相加
两数相加 题目 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字 如果,我们将这两个数相加起来,则会返回一个新的链表 ...
随机推荐
- HDU1548:A strange lift
A strange lift Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tota ...
- CodeForces 412D Giving Awards
根据给出的条件建边,然后进行dfs 对于某个点x,当x的后继都遍历完毕后,再输出x节点. 这样能保证所有约束条件. #include<cstdio> #include<cstring ...
- ubuntu中文论坛
http://forum.ubuntu.org.cn/index.php?sid=e40344219c81dbc4b289135a71db4efd
- Django之路:QuerySet API,后台和表单
一.Django QuerySet API Django模型中我们学习了一些基本的创建和查询.这里专门讲以下数据库接口相关的接口(QuerySet API),当然你也可以选择暂时跳过这节.如果以后用到 ...
- 注意:MainActivity的oncreate方法里不要再inflate布局了(MainActivity里的点击事件无响应)
activity_main已经通过setContentView(R.layout.activity_main);设置给MainActivity, 不要再inflate出新布局,然后findviewby ...
- C#入门经典第十章接口的实现
- 货物搬运(move)
货物搬运(move) 题目描述 天地无情人有情,一方有难八方支援!汶川大地震发生后,灾区最紧缺的是救灾帐篷,全国各地支援的帐篷正紧急向灾区运送.假设围绕纹川县有环行排列的n个救灾帐篷的存储点,每个存储 ...
- keyCode码集合
keycode 8 = BackSpace BackSpace keycode 9 = Tab Tab keycode 12 = Clear keycode 13 = Enter keycode 16 ...
- MyEclipse2014/2015 安装Activiti插件
一.废话 按照传统的教程而言,安装Activiti Designer 可以从 Help -> Install New Software Name:Activiti BPMN 2.0 design ...
- <c:foreach> 标签怎么获取循环次数?
<c:forEach var="i" begin="1" end="9" varStatus="status"&g ...