当前位置:网站首页>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();
}
};
边栏推荐
猜你喜欢

leetcode - 460. LFU cache

矩阵求逆操作的复杂度分析(逆矩阵的复杂度分析)

The interviewer asked four questions and summed up four experiences

【Image Processing】空间域图像增强

Create a nodejs based background service using express+mysql

Supplement 2: circle returning to origin

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

TextView文本大小自动适配与TextView边距的去除

【C语言-函数栈帧】从反汇编的角度,剖析函数调用全流程

leetcode - 230. The k-th smallest element in a binary search tree
随机推荐
1721. 交换链表中的节点
Installation (detailed illustration) and use of SVN
Matlab learning 9- nonlinear sharpening filter for image processing
【C语言-数据存储】数据在内存中是怎样存储的?
(2) Analysis of AAC source code from the perspective of architecture design - my livedata
【Image Processing】空间域图像增强
SQL基本查询
Interview question 02.02 Return the penultimate node
PVC 塑料片BS 476-6 火焰传播性能测定
Matlab r2022a installation tutorial
Create a nodejs based background service using express+mysql
Matlab learning 7- linear smoothing filtering of image processing
Sword finger offer 51 Reverse pair in array
2130. 链表最大孪生和
File system check of the root filesystem failed
欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?
端口占用问题,10000端口
206. 反转链表
Display DIN 4102-1 Class B1 fire test requirements
445. adding two numbers II