当前位置:网站首页>Leetcode 150. Evaluation of inverse Polish expression
Leetcode 150. Evaluation of inverse Polish expression
2022-07-19 12:14:00 【LuZhouShiLi】
Leetcode 150. Evaluate the inverse Polish expression
subject

Ideas
- Scan suffix expression , When you encounter a number, put it on the stack
- When the operator is scanned , Take out the two elements at the top of the stack for operation , Then store the results on the stack
Code
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
for(int i = 0; i < tokens.size(); i++)
{
if(tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/")
{
// Operator encountered Take out two top stack elements for operation Put the result back on the stack
int num1 = st.top();// Take out the top element of the stack
st.pop();// Out of the stack
int num2 = st.top();
st.pop();
if(tokens[i] == "+") st.push(num1 + num2);
if(tokens[i] == "-") st.push(num2 - num1);
if(tokens[i] == "*") st.push(num1 * num2);
if(tokens[i] == "/") st.push(num2 / num1);
}
else
{
st.push(stoi(tokens[i]));
}
}
int result = st.top();
st.pop();// Pop up the last element in the stack
return result;
}
};
边栏推荐
- [original] magisk+shamiko has been tested by app root
- Simple implementation of scrapy keyword crawler (take Xinhuanet and people's network as examples)
- HCIP (7)
- 微机原理与技术接口 实验五 基本IO操作温度控制实验
- 任天堂专利显示健身环配件后续产品或已在开发中
- Leetcode 150. 逆波兰表达式求值
- Focus on the new track of green development - release of MAPGIS intelligent environmental protection solution
- QT learning diary 17 - QT database
- ZABBIX SNMP monitoring
- Leetcode 20. 有效的括号
猜你喜欢

Familiar with nestjs (beginner)

C语言绘图示例-分色调图20例
![[wechat applet] use a thousand hand float - rollback](/img/52/4939ff2a644c46fdb388a64ac111c6.png)
[wechat applet] use a thousand hand float - rollback

01背包面试题系列(一)

HCIP(4)

The concept of binary tree and three traversal methods (C language)

HCIP(4)

psd. JS parsing PSD file
![[machine learning] evaluation index and code implementation of multi label classification](/img/01/7172841c663b5d9b79756e742bbc2c.png)
[machine learning] evaluation index and code implementation of multi label classification

Use native JS to realize the function of selecting all buttons, which is simple and clear
随机推荐
Property analysis of rotate matrix (forwarding)
Enabling cities to "plan, build, operate, manage and serve" -- MAPGIS CIM platform explores "cim+" multi scenario applications
文件一键备份
Introduction and Simulation Implementation of string class
谷歌开发者社区分享——Flutter动画的分享已发布
NPC, Microsoft, etc. proposed inclusivefl: inclusive federal learning on heterogeneous devices
Tikv thread pool performance tuning
Energy saving resource allocation strategy based on time-varying graph in satellite networks
Send blocking, receive blocking
LeetCode_ 216_ Combined sum III
js链式调用sleep函数 ------- <秋招打卡篇二>
zabbix-snmp监控
getchar()
Tiflash performance tuning
Genesis与BlueRun Ventures展开深度交流
人大、微软等提出InclusiveFL:异构设备上的包容性联邦学习
米哈游2023秋季招聘正式开始~提前批有机会免笔试!
LeetCode_77_组合
Microcomputer principle and technology Interface Experiment four subroutines and interrupt experiment
rman异机恢复报错RMAN-06026 RMAN-06023