当前位置:网站首页>Sword finger offer special assault edition day 12
Sword finger offer special assault edition day 12
2022-07-28 02:26:00 【hys__ handsome】
The finger of the sword Offer II 036. Postfix expression
The title directly gives the suffix expression , Just count it directly . If you give infixes, you need to convert suffixes to calculate ( The result can be obtained directly through calculation in the process of conversion , If you don't calculate directly in the process of transferring suffixes, you can directly push the symbols onto the stack , Then the final sequence string from the bottom of the stack to the top of the stack is the suffix expression )
class Solution {
public:
void cmp(stack<int> &nums, string op) {
int a = nums.top(); nums.pop();
int b = nums.top(); nums.pop();
if(op == "+") nums.push(b+a);
else if(op == "-") nums.push(b-a);
else if(op == "*") nums.push(b*a);
else nums.push(b/a);
}
int evalRPN(vector<string>& tokens) {
stack<int> nums;
for(string str: tokens) {
if(str == "+" || str == "-" || str == "*" || str == "/") {
cmp(nums,str);
} else nums.push(stoi(str));
}
return nums.top();
}
};
The finger of the sword Offer II 037. Asteroid collision
Stack simulation , Only the left side is positive , Only when the right side is negative can there be a collision .
class Solution {
public:
vector<int> asteroidCollision(vector<int>& asteroids) {
vector<int> stk;
for(auto num: asteroids) {
bool flag = true;
while(flag && num < 0 && !stk.empty() && stk.back() > 0) {
if(stk.back() > -num) flag = false;
else if(stk.back() == -num) {
// Both disappear at the same time
stk.pop_back();
flag = false;
} else stk.pop_back();
}
if(flag) stk.push_back(num);
}
return stk;
}
};
The finger of the sword Offer II 038. Daily temperature
Monotone stack optimization
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
int n = temperatures.size();
vector<int> res(n);
stack<int> sk;
for(int i = 0; i < n; i++) {
while(sk.size() && temperatures[sk.top()] < temperatures[i]) {
res[sk.top()] = i-sk.top();
sk.pop();
}
sk.push(i);
}
return res;
}
};
边栏推荐
- 【Star项目】小帽飞机大战(五)
- 【HCIP】BGP 特性
- LeetCode 热题 HOT 100 -> 1.两数之和
- How to evaluate the effectiveness of R & D personnel? Software Engineer reports help you see everyone's contribution
- 埃睿迪再度亮相数字中国峰会 持续深化用科技守护绿水青山
- [Yugong series] use of tabby integrated terminal in July 2022
- mysql创建存储过程---------[HY000][1418] This function has none of DETERMINISTIC, NO SQL
- 视频常用分辨率
- MySQL锁系列之锁算法详解(荣耀典藏版)
- Flex layout - fixed positioning + flow layout - main axis alignment - side axis alignment - expansion ratio
猜你喜欢

Appium 点击操作梳理

Leetcode hot topic Hot 100 - > 3. longest substring without repeated characters

ArcGIS: loading historical remote sensing images

使用BigDecimal类型应该避免哪些问题?(荣耀典藏版)

Flex development web page instance web side

软考 --- 数据库(2)关系模型

synchronized详解

智能合约安全——selfdestruct攻击

Read Plato & nbsp; Eplato of farm and the reasons for its high premium

Record a production deadlock
随机推荐
【网站搭建】使用acme.sh更新ssl证书:将zerossl改为letsencrypt
Zkrollup learning materials summary
【HCIP】路由策略、策略路由
How MySQL uses indexes (glory Collection Edition)
Leetcode hot topic Hot 100 - > 3. longest substring without repeated characters
MySQL's way to solve deadlock - lock analysis of common SQL statements
Unity saves pictures to albums and rights management
go 学习02 基础知识
【ROS进阶篇】第九讲 基于Rviz和Arbotix控制的机器人模型运动
Class notes (5) (1) - 593. Binary search
In it, there is a million talent gap, and the salary rises, but it is not capped
网络必知题目
[机缘参悟-53]:阳谋立身,阴谋防身
【ROS进阶篇】第十讲 基于Gazebo的URDF集成仿真流程及实例
Which database is the fastest to query data only?
软考 --- 数据库(2)关系模型
Aike AI frontier promotion (7.14)
智能合约安全——selfdestruct攻击
mysql创建存储过程---------[HY000][1418] This function has none of DETERMINISTIC, NO SQL
考研数学一元微分学证明题常见题型方法