当前位置:网站首页>Sword finger offer II 036 Postfix Expression
Sword finger offer II 036 Postfix Expression
2022-06-11 08:56:00 【Drag is me】
leetcode Force button to brush questions and punch in
subject : The finger of the sword Offer II 036. Postfix expression
describe : according to Reverse Polish notation , Find the calculation result of the suffix expression .
Valid operators include +、-、*、/ . Each operand can be an integer , It can also be another inverse Polish expression .
explain :
Integer division keeps only the integer part .
Given an inverse Polish expression is always valid . let me put it another way , The expression always yields a valid number and does not have a divisor of 0 The situation of .
The inverse Polish expression has two main advantages :
There is no ambiguity in the expression after removing the brackets , Even if the above formula is written as 1 2 + 3 4 + * You can also calculate the correct result according to the order .
It is suitable for stack operation : When it comes to numbers, it's on the stack ; When you encounter an operator, you take out two numbers at the top of the stack to calculate , And push the results into the stack .
Their thinking
1、 All the questions are told to use the stack ;
2、 Determine whether the string is a number ;
Source code ##
class Solution {
public:
bool isnum(string s) {
return !(s == "+" || s == "-" || s == "*" || s == "/");
}
int evalRPN(vector<string>& tokens) {
stack<int>s;
int pre1 = 0, pre2 = 0, ans = 0, temp = 0;
for (int i = 0; i < tokens.size(); ++i) {
if (isnum(tokens[i])) {
temp = stoi(tokens[i]);
s.push(temp);
} else {
pre1 = s.top();
s.pop();
pre2 = s.top();
s.pop();
if (tokens[i][0] == '+') {
s.push(pre1 + pre2);
} else if (tokens[i][0] == '-') {
s.push(pre2 - pre1);
} else if (tokens[i][0] == '*') {
s.push(pre1 * pre2);
} else if (tokens[i][0] == '/') {
s.push(pre2 / pre1);
}
}
}
return s.top();
}
};
边栏推荐
- BS 7176软垫家具阻燃防火测试
- Supplement 2: circle returning to origin
- 2095. delete the intermediate node of the linked list
- Matlab学习9-图像处理之非线性锐化滤波
- What is the process of en 1101 flammability test for curtains?
- Sword finger offer 18 Delete the node of the linked list
- EN 45545-2T10水平法烟密度检测的注意事项
- typescript高阶特性一 —— 合并类型(&)
- Analysis of EN 45545 R24 oxygen index test method
- What are precompiled, compiled, assembled, linked, static and dynamic libraries
猜你喜欢

Sword finger offer 10- ii Frog jumping on steps

Sword finger offer 21 Adjust array order so that odd numbers precede even numbers

leetcode - 460. LFU cache

Intelligent control theory question bank

C语言打印菱形

Notes on MySQL core points

M1 chip guide: M1, M1 pro, M1 Max and M1 ultra

What if the copied code format is confused?

c语言打印心形

Supplement 2: circle returning to origin
随机推荐
室内膨胀型防火涂料根据BS 476-21 耐火标准测定需要符合几项?
Supplement 2: circle returning to origin
Getting started with Zipkin
2130. 链表最大孪生和
2095. delete the intermediate node of the linked list
SAP abap 字段符号
Wood board ISO 5660-1 heat release rate mapping test
CMVSS TSD No. 302与49 CFR 571.302测试方法是否一样
SAP abap 数据类型与数据对象
Installation (detailed illustration) and use of SVN
83. 删除排序链表中的重复元素
完整的ES6面试题
Are the test methods of CMVSS TSD No. 302 and 49 CFR 571.302 the same
Create a nodejs based background service using express+mysql
面试官问了四个问题,总结了4个经验
leveldb简单使用样例
C language printing heart
显示器要申请BS 476-7 怎么送样?跟显示屏一样吗
面试题 02.02. 返回倒数第 k 个节点
标准化编写知识