当前位置:网站首页>735. 行星碰撞 : 简单栈模拟运用题
735. 行星碰撞 : 简单栈模拟运用题
2022-07-13 16:56:00 【宫水三叶的刷题日记】
题目描述
这是 LeetCode 上的 735. 行星碰撞 ,难度为 中等。
Tag : 「DFS」、「模拟」、「哈希表」
给定一个整数数组 asteroids,表示在同一行的行星。
对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动)。每一颗行星以相同的速度移动。
找出碰撞后剩下的所有行星。碰撞规则:两个行星相互碰撞,较小的行星会爆炸。如果两颗行星大小相同,则两颗行星都会爆炸。两颗移动方向相同的行星,永远不会发生碰撞。
示例 1:
输入:asteroids = [5,10,-5]
输出:[5,10]
解释:10 和 -5 碰撞后只剩下 10 。 5 和 10 永远不会发生碰撞。
示例 2:
输入:asteroids = [8,-8]
输出:[]
解释:8 和 -8 碰撞后,两者都发生爆炸。
示例 3:
输入:asteroids = [10,2,-5]
输出:[10]
解释:2 和 -5 发生碰撞后剩下 -5 。10 和 -5 发生碰撞后剩下 10 。
提示:
模拟 + 栈
为了方便,我们令 asteroids 为 ats。
由于碰撞抵消总是从相邻行星之间发生,我们可以使用「栈」来模拟该过程。
从前往后处理所有的 ,使用栈存储当前未被抵消的行星,当栈顶元素方向往右,当前 方向往左时,会发生抵消操作,抵消过程根据规则进行即可。
Java 代码:
class Solution {
public int[] asteroidCollision(int[] ats) {
Deque<Integer> d = new ArrayDeque<>();
for (int t : ats) {
boolean ok = true;
while (ok && !d.isEmpty() && d.peekLast() > 0 && t < 0) {
int a = Math.abs(d.peekLast()), b = Math.abs(t);
if (a <= b) d.pollLast();
if (a >= b) ok = false;
}
if (ok) d.addLast(t);
}
int sz = d.size();
int[] ans = new int[sz];
while (!d.isEmpty()) ans[--sz] = d.pollLast();
return ans;
}
}
TypeScript 代码:
function asteroidCollision(ats: number[]): number[] {
const stk: number[] = new Array<number>()
for (const t of ats) {
let ok: boolean = true
while (ok && stk.length > 0 && stk[stk.length - 1] > 0 && t < 0) {
const a = Math.abs(stk[stk.length - 1]), b = Math.abs(t);
if (a <= b) stk.pop()
if (a >= b) ok = false
}
if (ok) stk.push(t)
}
return stk
};
Python3 代码:
class Solution:
def asteroidCollision(self, ats: List[int]) -> List[int]:
stk = []
for t in ats:
ok = True
while ok and stk and stk[-1] > 0 and t < 0:
a, b = abs(stk[-1]), abs(t)
if a <= b:
stk.pop(-1)
if a >= b:
ok = False
if ok:
stk.append(t)
return stk
时间复杂度: 空间复杂度:
最后
这是我们「刷穿 LeetCode」系列文章的第 No.735 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地
本文由 mdnice 多平台发布
边栏推荐
- Stack栈的三种含义
- Xilinx Vitis
- 主席树复习
- Is the sub database and sub table really suitable for your system? Talk about how to select sub databases, sub tables and newsql
- QT designer sets the background and background picture
- 面试题 08.04. 幂集
- gcc __ attribute__ Keyword example visibility
- Qt Designer设置背景以及背景图片
- tensorflow 使用 深度学习(二)
- Use of listview and recyclerview
猜你喜欢

一篇文章带你了解国企程序员(超详细)

DCC888 :Instruction Level Parallelism

测试/开发程序员的薪资不平衡?忙碌生活各种跳槽......

重磅:国产IDE发布,由阿里研发,完全开源(高性能+高定制性)

C language custom type chapter - custom type: structure, enumeration, union

【回归预测-LSTM】基于attention机制的LSTM实现时间序列回归预测附matlab代码

Is the sub database and sub table really suitable for your system? Talk about how to select sub databases, sub tables and newsql

一群南大学子靠科技出海,年入10亿
![[route optimization] HWSN energy-saving clustering protocol based on biogeography optimization with matlab code](/img/56/c3e4ada26101d243a5c1aa840f56ba.png)
[route optimization] HWSN energy-saving clustering protocol based on biogeography optimization with matlab code

分库分表真的适合你的系统吗?聊聊分库分表和NewSQL如何选择
随机推荐
How the Internet loads kernel modules
SQL Server 中的异常处理
Introduction to C language (4)
【日常训练】735. 行星碰撞
螺旋矩阵
Technology sharing | common proxy tools for interface testing
OSPF综合实验(7.12)
【数字识别】基于知识库实现手写体数字识别附matlab代码
[Beijing Forestry University] information sharing of the first and second postgraduate examinations
进制转换
一个FlinkSQL 脚本 可以写两个表的insert语句吗?
[daily training] 735 Planetary collision
Dynamic + shardingsphere (4.1.1) realizes dynamic database and table division
FLASH W74M12JWSSIQ_ W25q64fwzpig specification, memory
draw.io图片保存路径设置
Hexadecimal conversion
怎样设计接口?
gcc __ attribute__ Keyword example visibility
数字藏品大热,年轻人快不够用了
Omnivore, a non picky AI model, focuses on images, videos and 3D data