当前位置:网站首页>Leetcode 735 planetary collision [stack simulation] the leetcode road of heroding
Leetcode 735 planetary collision [stack simulation] the leetcode road of heroding
2022-07-16 08:24:00 【HERODING23】

Their thinking :
A more complex simulation problem , First of all, understand , The condition of planetary collision is that the speed of the planet on the left is positive , Only when the speed of the planets on the right is negative can they collide , And continuous collisions can occur , That is, the planet on the right is left and larger than the planet on the left and right , Collide until you encounter a planet larger than the one on the right, or the same size, or no planet on the right , So use stack to store . When traversing , First judge the direction , If you walk in the opposite direction ,while The cycle begins to collide , End of collision , Finally, if the opposite size is the same, they all disappear , There is no right planet to put the current planet into , Finally, traverse the stack and put vector And reverse , The code is as follows :
class Solution {
public:
vector<int> asteroidCollision(vector<int>& asteroids) {
stack<int> s;
vector<int> ans;
for(auto& asteroid : asteroids) {
if(s.empty()) {
s.push(asteroid);
} else {
int top = s.top();
if(top > 0 && asteroid < 0) {
while(top > 0 && asteroid + top < 0) {
s.pop();
if(s.empty()) {
break;
}
top = s.top();
}
if(top + asteroid == 0) {
s.pop();
continue;
}
if(s.empty() || (!s.empty() && s.top() < 0)) {
s.push(asteroid);
}
} else {
s.push(asteroid);
}
}
}
while(!s.empty()) {
ans.push_back(s.top());
s.pop();
}
reverse(ans.begin(), ans.end());
return ans;
}
};
边栏推荐
- Force buckle 732 My schedule III
- [U - boot] u - boot Sandbox compilation Construction and use Summary
- (2.4)【服务型木马-SlimFTP】介绍、使用
- 测试基础1
- 研发中台拆分过程的一些心得总结
- C primer plus learning notes - 4. File IO (input / output)
- [daily question 1] find the nearest common ancestor of two nodes in the binary tree
- London Metal real-time market and operation skills
- Send your code into space and develop "the greatest work" together
- 微信小程序页面的跳转和导航的配置和vant组件
猜你喜欢

After some experiments, the metaphysics of batch size was broken

解决GD32F20X支持包安装后打开官方例程无法识别芯片问题

将 Terraform 生态粘合到 Kubernetes 世界

Mobile automation uses commands to view the app package name

ora-01153

Day102.尚医通项目
![[U - boot] u - boot Sandbox compilation Construction and use Summary](/img/5f/92d0a1937230d00b4068d65a5a6c29.png)
[U - boot] u - boot Sandbox compilation Construction and use Summary

【每日一题】在二叉树中找到两个节点的最近公共祖先

(手工)【sqli-labs27、27a】报错回显、布尔盲注、过滤后注入

猫狗分类-VGG16-Finetune
随机推荐
Tc397 lmuram use
Kubernetes 问题排查全景图
Traversal before, during and after binary tree
Leetcode 454. 四数相加 II
Configuration and vant component of jump and navigation of wechat applet page
Catégorisation des chiens et des chats - vgg16 bottleeck
杰理之IR 可能出现丢码问题【篇】
可观测|时序数据降采样在Prometheus实践复盘
Kubernetes Dashboard
Small program graduation project of wechat enterprise company (1) development outline
C primer plus学习笔记 —— 4、文件IO(输入/输出)
Wechat classroom appointment of applet completion works applet graduation design (2) applet function
测试基础2
ora-01153
mySQL上的应用了解
小程序毕设作品之微信企业公司小程序毕业设计(1)开发概要
深入浅出 eBPF|你要了解的 7 个核心问题
STM32实现NRF24L01通信
MySQL basic query statement
Differences and relations between malloc, vmalloc and kmalloc, free, kfree and vfree