当前位置:网站首页>Sword finger offer II 037 Asteroid collision
Sword finger offer II 037 Asteroid collision
2022-06-30 00:18:00 【Small white yards fly up】
Summary
Use the stack to process whether two adjacent planets collide .
subject
Given an array of integers asteroids, Represents asteroids on the same line .
For each element in the array , Its absolute value represents the size of the asteroid , Positive and negative indicate the direction of asteroid movement ( Positive means moving to the right , Negative means moving to the left ). Every asteroid moves at the same speed .
Find all the asteroids left after the collision . Collision rules : The two planets collide with each other , Smaller planets will explode . If two planets are the same size , Then both planets will explode . Two planets moving in the same direction , There will never be a collision .

link :https://leetcode.cn/problems/XagZNi
Ideas
I little interesting .
It's actually , Compare two adjacent values , Look at the sign first .
- If the symbols are the same , All remain .
- If the symbols are reversed
- The former is negative , The latter is positive , Then the two do not conflict , All reserved .
- The former is positive , The latter is negative , Then the number with the largest absolute value will be reserved .
The first idea is , Can be matched in an array to handle , But for removing elements from an array , Or reuse the elements that appeared before , It's still a bit of a hassle .
The key is , The asteroid you met in the back , Will continue to hit you .
The stack is still used here .
solution : Stack
Code
public int[] asteroidCollision(int[] asteroids) {
Stack<Integer> stack = new Stack<>();
int i = 0;
while (i < asteroids.length) {
int current = asteroids[i];
// When the stack is empty , Direct stack
if (stack.size() == 0) {
stack.push(current);
i++;
continue;
}
int peek = stack.peek();
if (peek > 0 && current < 0) {
// A collision occurs
int result = peek + current;
// Compare their absolute values
if (result < 0) {
// Stack top absolute value is small , Then it was collided and exploded , Out of the stack
stack.pop();
} else if (result == 0) {
// perish together
stack.pop();
i++;
} else {
i++;
}
} else {
stack.push(current);
i++;
}
}
int[] result = new int[stack.size()];
for (int j = result.length - 1; j >= 0; j--) {
result[j] = stack.pop();
}
return result;
}
Submit results

The above judgment is too verbose , Simplify the code , as follows :
public int[] asteroidCollision(int[] asteroids) {
Stack<Integer> stack = new Stack<>();
int i = 0;
while (i < asteroids.length) {
int current = asteroids[i];
// When the stack is empty , Direct stack
if (stack.isEmpty() || stack.peek() < 0 || current > 0) {
stack.push(current);
i++;
continue;
}
int peek = stack.peek();
// Only the top of the stack is positive , The current element is negative , The collision will happen
if (peek <= -current) {
// Stack top absolute value is small , Then it was collided and exploded , Out of the stack
stack.pop();
if (peek < -current) {
continue;
}
}
i++;
}
int[] result = new int[stack.size()];
for (int j = result.length - 1; j >= 0; j--) {
result[j] = stack.pop();
}
return result;
}

Ah, this , Time and memory have increased ……
边栏推荐
- What is IGMP? What is the difference between IGMP and ICMP?
- 俞敏洪:我的退与进;架构师必须了解的5种最佳软件架构模式;Redis夺命52连问|码农周刊VIP会员专属邮件周报 Vol.096
- [advanced C language] special user-defined type
- Table responsive layout tips for super nice
- 《性能之巅第2版》阅读笔记(四)--Memory监测
- 剑指 Offer II 035. 最小时间差
- Andorid source build/envsetup. SH details to know
- 8 software engineering environment
- Solr basic operation 11
- Quick Pow: 如何快速求幂
猜你喜欢

Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus

云呐|如何利用系统管理固定资产?如何进行固定资产管理?

js中的事件

JS draw polar color gradient

公司固定资产该哪个部门管理,一般公司固定资产怎么管理
![克隆无向图[bfs访问每条边而不止节点]](/img/34/2a1b737b6095293f868ec6aec0ceeb.png)
克隆无向图[bfs访问每条边而不止节点]

爬虫入门实战:斗鱼弹幕数据抓取,附送11节入门笔记
![Cloner un Graphe non recté [bfs accède à chaque bord et pas seulement aux noeuds]](/img/34/2a1b737b6095293f868ec6aec0ceeb.png)
Cloner un Graphe non recté [bfs accède à chaque bord et pas seulement aux noeuds]

8软件工程环境

QT learning 05 introduction to QT creator project
随机推荐
500 error occurred after importing skins folder into solo blog skin
Solr基础操作12
蛇形矩阵(数组模拟方向, d代表转弯)
基于zfoo开发项目的一些规范
Solr basic operation 11
云呐|固定资产系统管理,nc系统管理固定资产在哪里
gyctf_2020_document
MySQL functions and constraints
What does it mean to open an account online? In addition, is it safe to open an account online now?
Basic tutorial for installing monggodb in win10
How to view the CPU cores and threads in win11? Win11 view the tutorial of how many cores and threads the CPU is
[advanced C language] dynamic memory management
【UITableView】坑一:tableView:heightForHeaderInSection:方法不执行
Fine grained identification, classification, retrieval data set collation
JS绘制极坐标颜色渐变
Koa2 learning and using
爬虫入门实战:斗鱼弹幕数据抓取,附送11节入门笔记
Buffer flow exercise
Summarize Flink runtime architecture in simple terms
Some specifications based on zfoo development project