当前位置:网站首页>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 ……
边栏推荐
- Stack space of JVM
- QT learning 01 GUI program principle analysis
- Mysql:sql overview and database system introduction | dark horse programmer
- Unity splashimage scaling problem
- Solr basic operation 16
- Solr basic operation 8
- [QNX Hypervisor 2.2用户手册]6.2.2 Guest与Host之间通信
- 【UITableView】坑一:tableView:heightForHeaderInSection:方法不执行
- Web APIs 环境对象 丨黑马程序员
- SSH key disclosure (module B competition topic) -- Application Service Vulnerability scanning and utilization
猜你喜欢

01 backpack problem

由GlideException: Failed DecodePath{DirectByteBuffer->GifDrawable->Drawable}引起的刨根问底

What is IGMP? What is the difference between IGMP and ICMP?

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

蛇形矩阵(数组模拟方向, d代表转弯)
500 error occurred after importing skins folder into solo blog skin

After 8 years of polishing, "dream factory of game design" released an epic update!

Activity invitation | the Apache Doris community essay and speech solicitation activity has begun!

8软件工程环境

About mongodb error: connecting to: mongodb://127.0.0.1:27017/?compressors=disabled &gssapiServiceName=mongodb
随机推荐
Mysql:sql overview and database system introduction | dark horse programmer
Exploration and Practice on the future direction of byte cloud database
Finding a job in 2022 is the "last lesson" for graduates
爬虫入门实战:斗鱼弹幕数据抓取,附送11节入门笔记
500 error occurred after importing skins folder into solo blog skin
What is IGMP? What is the difference between IGMP and ICMP?
Solr基础操作14
JS的初步语法
[advanced C language] file operation (I)
面试官:为什么数据库连接很消耗资源?我竟然答不上来。。一下懵了!
Majority element ii[molar voting method for finding modes]
Several simple queries of SQL Server database
Vulnhub target -moriartycorp
JS绘制极坐标颜色渐变
Review of vsftp, TFTP, samba and NFS
Solr基础操作15
Stream collectors usage
Rotating colored clover
label问题排查:打不开标注好的图像
QT learning 07 coordinate system in QT