当前位置:网站首页>剑指 Offer II 037. 小行星碰撞
剑指 Offer II 037. 小行星碰撞
2022-06-30 00:04:00 【小白码上飞】
概要
用栈处理相邻两颗行星是否碰撞。
题目
给定一个整数数组 asteroids,表示在同一行的小行星。
对于数组中的每一个元素,其绝对值表示小行星的大小,正负表示小行星的移动方向(正表示向右移动,负表示向左移动)。每一颗小行星以相同的速度移动。
找出碰撞后剩下的所有小行星。碰撞规则:两个行星相互碰撞,较小的行星会爆炸。如果两颗行星大小相同,则两颗行星都会爆炸。两颗移动方向相同的行星,永远不会发生碰撞。

链接:https://leetcode.cn/problems/XagZNi
思路
有点意思。
实际上是,比较相邻的两个值,先看正负号。
- 如果符号相同,则都保留。
- 如果符号相反
- 前者为负,后者为正,则两不冲突,都保留。
- 前者为正,后者为负,则绝对值大的那个数字保留。
第一个想法是,可以在数组中两两匹配去处理,但是对于数组的移除元素,或者重复利用之前出现的元素,还是比较麻烦的。
最关键的是,你在后面遇到的小行星,还会继续往前撞你呀。
这里还是使用栈。
解法:栈
代码
public int[] asteroidCollision(int[] asteroids) {
Stack<Integer> stack = new Stack<>();
int i = 0;
while (i < asteroids.length) {
int current = asteroids[i];
// 栈空的时候,直接入栈
if (stack.size() == 0) {
stack.push(current);
i++;
continue;
}
int peek = stack.peek();
if (peek > 0 && current < 0) {
// 发生碰撞的情况
int result = peek + current;
// 比较二者绝对值
if (result < 0) {
// 栈顶绝对值小,则被碰撞爆炸,出栈
stack.pop();
} else if (result == 0) {
// 同归于尽
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;
}
提交结果

上面的判断过于啰嗦,简化了一下代码,如下:
public int[] asteroidCollision(int[] asteroids) {
Stack<Integer> stack = new Stack<>();
int i = 0;
while (i < asteroids.length) {
int current = asteroids[i];
// 栈空的时候,直接入栈
if (stack.isEmpty() || stack.peek() < 0 || current > 0) {
stack.push(current);
i++;
continue;
}
int peek = stack.peek();
// 只有栈顶为正数,当前元素为负数,才会发生碰撞的情况
if (peek <= -current) {
// 栈顶绝对值小,则被碰撞爆炸,出栈
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;
}

啊这,耗时和内存都增加了……
边栏推荐
- This PMP Exam (June 25), some people are happy and others are worried. That's why
- Siemens low code version 9.14: meet different needs
- [advanced C language] string and memory function (I)
- 多数元素II[求众数类之摩尔投票法]
- Clone undirected graph [bfs accesses each edge but not only nodes]
- Solr basic operation 8
- Andorid source build/envsetup. SH details to know
- Ingenious application of golang generics to prevent null pointer errors of variables and structural fields
- Summarize Flink runtime architecture in simple terms
- 请指教同花顺软件究竟是什么?究竟网上开户是否安全么?
猜你喜欢

Siemens low code platform connects MySQL through database connector to realize addition, deletion, modification and query
500 error occurred after importing skins folder into solo blog skin

QT learning 01 GUI program principle analysis

Summarize Flink runtime architecture in simple terms

云原生爱好者周刊:炫酷的 Grafana 监控面板集合

Common knowledge of ECS security settings

AI首席架构师9-胡晓光 《飞桨模型库与行业应用》

【微信小程序】认识小程序项目的基本组成结构
![[rust weekly library] Tokei - a utility for statistics of code lines and other information](/img/6c/4569cc0edaa01e4605c9c256193c31.png)
[rust weekly library] Tokei - a utility for statistics of code lines and other information

This simple little function saves 213 hours for our production research team in half a year
随机推荐
Solr basic operations 7
Activity invitation | the Apache Doris community essay and speech solicitation activity has begun!
HTAP x cloud native: tidb accelerates the release of data value and realizes data agility
What is flush software? Is it safe to open an account online?
Vulnhub靶机-MoriartyCorp
gyctf_ 2020_ document
Sword finger offer 13 Range of motion of robot
【微信小程序】认识小程序项目的基本组成结构
这次的PMP考试(6月25日),有人欢喜有人忧,原因就在这...
modelsim的TCL脚本的define incdir命令解析
JS绘制极坐标颜色渐变
Teach you step by step to install webwind notes on edge browser
[wechat applet] understand the basic composition of the applet project
Solr basic operations 13
Halcon practical: design idea of solder joint detection
Web APIs environment object - dark horse programmer
Analysis of define incdir command in TCL script of Modelsim
西门子低代码平台通过Database Connector 连接Mysql 实现增删改查
基于zfoo开发项目的一些规范
Siemens low code platform connects MySQL through database connector to realize addition, deletion, modification and query