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:两数相加
两数相加 题目 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字 如果,我们将这两个数相加起来,则会返回一个新的链表 ...
随机推荐
- javascript语句语义大全(7)
1. 事件 onmousedown——鼠标按下事件 当鼠标按下的时候触发,根据鼠标不同的按键会有不同的值传入,左键0,滚轮1,右键2,不同浏览器可能有不同. onmousemove——当鼠标移动的时候 ...
- k-Means和KNN算法简述
k-means 算法 k-means 算法接受输入量 k :然后将n个数据对象划分为 k个聚类以便使得所获得的聚类满足:同一聚类中的对象相似度较高:而不同聚类中的对象相似度较小.聚类相似度是利用各聚类 ...
- Ubuntu + Django + Nginx + uwsgi
环境 Ubuntu 14.04 Python 2.7 Django 1.8.4 1 安装Nginx sudo apt-get install nginx 测试 sudo /etc/init. ...
- [IDL入门] 两个PPT,IDL上手
首先看看IDL能干什么,<Solving Real Problems with Computer Graphics>ppt是英文的,很精彩. 下载地址:http://pan.baidu.c ...
- VBS脚本随笔
1.定时运行程序与关闭程序的VBS处理方法: do set ws=createobject("wscript.shell") ws.run"你要运行的程序的路径(比如说d ...
- 服务器 vps 空间
服务器 服务器是一台独立的机器,拥有独立的ip,磁盘空间,内存空间,可以认为是一台功能强大的电脑. VPS VPS是利用虚拟化技术将服务器虚拟成多台服务器,独立的ip,磁盘空间,内存空间,服务器的vp ...
- linux export将PATH环境变量误删了的解决办法
今天新增环境变量的时候不小心把冒号错打成了分号 export PATH=/usr/local/php5/bin;$PATH; 导致PATH变量为/usr/local/php/bin 解决办法:[ubu ...
- jsp如果超过字数就变成...
<script> var infoTitle = '<ww:property value="infoTitle"/>'; if(infoTitle.leng ...
- CentOS6.5 添加epel源
0.安装yum优先级插件 yum install yum-priorities 1.epel简介: https://fedoraproject.org/wiki/EPEL/zh-cn rpm -Uvh ...
- LoadRunner 录制cas 登陆脚本
关于CAS 的概念,见链接:http://www.360doc.com/content/15/0204/17/21706453_446251626.shtml 需要增加4个关联函数 //Correla ...