当前位置:网站首页>剑指offer专项突击版第12天
剑指offer专项突击版第12天
2022-07-28 00:51:00 【hys__handsome】
剑指 Offer II 036. 后缀表达式
题目直接给了后缀表达式,就直接算就行了。如果给的是中缀那么就需要转后缀再计算(转换的过程中计算可以直接拿到结果,若转后缀过程中不直接计算直接把符号也压入栈中,那么最终得到的从栈底到栈头的次序字符串就是后缀表达式)
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();
}
};
栈模拟,只有左边为正,右边为负才有可能发生碰撞。
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) {
//两都同时消失
stk.pop_back();
flag = false;
} else stk.pop_back();
}
if(flag) stk.push_back(num);
}
return stk;
}
};
单调栈优化
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;
}
};
边栏推荐
- Fluorite network, difficult to be a "lone brave"
- Fiddler mobile packet capturing agent settings (for Huawei glory 60s)
- Execute add migration migration and report build failed
- 软件测试面试题:为什要在一个团队中开展测试工作?
- Implementation of mongodb/mongotemplate.upsert batch inserting update data
- 执行 Add-Migration 迁移时报 Build failed.
- Principle and implementation of cross entropy
- sftp文件/文件夹上传服务器
- Netease cloud copywriting
- 记录一次生产死锁
猜你喜欢

Behind every piece of information you collect, you can't live without TA

Huawei app UI automation test post interview real questions, real interview experience.

go 学习02 基础知识

【数据库数据恢复】SQL Server数据库磁盘空间不足的数据恢复案例

Promise从入门到精通 (第2章 Promise的理解和使用)

Unreal ue4.27 switchboard porting engine process

云原生爱好者周刊:Prometheus 架构演进之路

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

go 学习01

CeresDAO:Ventures DAO的“新代言”
随机推荐
WMS you don't know
Likeshop takeout ordering system [100% open source, no encryption]
MPLS tunnel experiment
Under the new retail format, retail e-commerce RPA helps reshape growth
11 Django basics database operation
Netease cloud copywriting
你所不知道的WMS
Linux Installation mysql8.0.29 detailed tutorial
Five basic data structures of redis
测试/开发程序员的级别“陷阱“,级别不是衡量单维度的能力......
Enterprise operation and maintenance practice - using aliyun container image service to pull and build images of overseas GCR and quay warehouses
A lock faster than read-write lock. Don't get to know it quickly
记录一次生产死锁
Forget the root password
MySQL高可用和主从同步
They are all talking about Devops. Do you really understand it?
Starfish OS X metabell strategic cooperation, metauniverse business ecosystem further
Go learn 02 basic knowledge
软件测试面试题:请详细介绍一下各种测试类型的含义?
损失函数-交叉熵的原理及实现