150. Evaluate Reverse Polish Notation (Stack)
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
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(vector< string > &tokens) {
stack< int > operandStack;
int operand1;
int operand2;
for(int i = ; i < tokens.size(); i++){
if(tokens[i]=="+"){
operand1 = operandStack.top();
operandStack.pop();
operand2 = operandStack.top();
operandStack.pop();
operand2 += operand1;
operandStack.push(operand2);
}
else if(tokens[i]=="-"){
operand1 = operandStack.top();
operandStack.pop();
operand2 = operandStack.top();
operandStack.pop();
operand2 -= operand1;
operandStack.push(operand2);
}
else if(tokens[i]=="*"){
operand1 = operandStack.top();
operandStack.pop();
operand2 = operandStack.top();
operandStack.pop();
operand2 *= operand1;
operandStack.push(operand2);
}
else if(tokens[i]=="/"){
operand1 = operandStack.top();
operandStack.pop();
operand2 = operandStack.top();
operandStack.pop();
operand2 /= operand1;
operandStack.push(operand2);
}
else{
operand1 = atoi(tokens[i].c_str());
operandStack.push(operand1);
}
}
return operandStack.top();
}
};
150. Evaluate Reverse Polish Notation (Stack)的更多相关文章
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 150. Evaluate Reverse Polish Notation - LeetCode
Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- 【刷题-LeetCode】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode OJ 150. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- leetcode 150. Evaluate Reverse Polish Notation ------ java
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 150. Evaluate Reverse Polish Notation逆波兰表达式
[抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...
- [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
随机推荐
- wordpress重力表单实时提醒功能教程(亲测可用)
小七在写项目的时候遇到了一个需求:用户在填写完成表单的各个字段后要提交到后台,但是后台程序狗不能一直守着后台吧,程序狗也需要陪女朋友啊,好做一个即时提醒的功能吧,再也不担心用户提交的内容被错过了,第一 ...
- docker怎么破?
为什么要装docker? 因为linux服务器不好用,很多操作不好进行,比如安装包没有管理员权限 docker可以访问本地显卡,比一般的virtual box 或者VMware都要好 怎么装docke ...
- SublimeText3搭建go语言开发环境(windows)
SublimeText3搭建go语言开发环境(windows) 下载并解压: Sublime Text Build 3021.zip注册: 尽量不要去破解 安装Package C ...
- appium+python自动化37-adb shell模拟点击事件(input tap)
前言 appium有时候定位一个元素很难定位到,或者说明明定位到这个元素了,却无法点击,这个时候该怎么办呢? 求助大神是没用的,点击不了就是点击不了,appium不是万能的,这个时候应该转换思路,换其 ...
- Java运算符 逻辑运算符 短路运算符
& 与 两个运算数都为真时结果为真,只要有一个运算数为假结果就为假,否则就为真. true & true = true true & false = false ...
- Oracle profile含义、修改、新增
profiles文件是口令和资源限制的配置集合,包括CPU的时间.I/O的使用.空闲时间.连接时间.并发会话数量.密码策略等对于资源的使用profile可以做到控制会话级别或语句调用级别.oracle ...
- Java实现HTML转换为PDF的常见方法
最近在自己的项目中需要动态生成融资单合同,这里需要把对应的html转换为对应的pdf融资合同.因此需要通过Java实现将HTML转PDF.自己之前没有接触过这一块的东西,所以上网查了一下,网上有很多的 ...
- OD 实验(十七) - 对一个程序的逆向分析
程序: 运行程序 弹出一个对话框,点击 OK 来到主界面,点击 Help -> Register Now 这是输入注册码的地方 按关闭程序的按钮 会提示剩下 30 天的使用时间 用 Ressco ...
- 为什么要使用叶脊(leaf-spine)拓扑网络?
三层网络设计的结构发展已经很成熟,但leaf-spine (leaf叶节点,spine脊节点)结构越来越热门,网络设计师们应该如何进行选择呢? 尽管三层网络结构应用广泛而且技术成熟,但随着技术的发展, ...
- LinkedHashMap学习
一.概述 LinkedHashMap继承自HashMap,是Map接口的一个具体实现,它是有序的,可以按照插入顺序先后和访问时间先后进行排序,选择哪种排序方式取决于在新建LinkedHashMap的时 ...