当前位置:网站首页>2022.07.13 _ a day
2022.07.13 _ a day
2022-07-31 07:40:00 【No. い】
735. 行星碰撞
题目描述
给定一个整数数组 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 .
提示:
2 <= asteroids.length <= 104
-1000 <= asteroids[i] <= 1000
asteroids[i] != 0
- 栈
- 数组
coding
// 栈模拟
class Solution {
public int[] asteroidCollision(int[] asteroids) {
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < asteroids.length; i++) {
if (stack.isEmpty() || stack.peek() * asteroids[i] > 0 || stack.peek() < 0) {
// 1.栈为空
// 2.The planet at the top of the stack moves in the same direction as the next planet
// 3.The planet at the top of the stack moves to the left (Even if the movement is oriented differently,This is equivalent to left facing left,right to right,不会发生碰撞)
stack.push(asteroids[i]);
} else {
// When two stars move towards each other,If the right-facing planet in the stack has a higher velocity,Then the new star explodes directly,Then look at the situation of the next planet
// 否则,Planets in the stack must explode
if (stack.peek() <= - asteroids[i]) {
// If the speed of the top planet is small,The newly added planets are still considered
if (stack.peek() < - asteroids[i]) {
i --;
}
// If the speed is the same, the planet at the top of the stack and the planet that needs to be added will die together
stack.pop();
}
}
}
int[] res = new int[stack.size()];
for (int i = res.length - 1; i >= 0; i--) {
res[i] = stack.pop();
}
return res;
}
}
边栏推荐
- Detailed explanation of js prototype
- HighTec 的安装与配置
- 【微服务】(十六)—— 分布式事务Seata
- Core Tower Electronics won the championship in the Wuhu Division of the 11th China Innovation and Entrepreneurship Competition
- 360 push-360 push tool-360 batch push tool
- 从入门到一位合格的爬虫师,这几点很重要
- 【解决】npm ERR A complete log of this run can be found in npm ERR
- 【第四章】详解Feign的实现原理
- 高并发与多线程之间的难点对比(容易混淆)
- 03-SDRAM:写操作(突发)
猜你喜欢
【微服务】Nacos集群搭建以及加载文件配置
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
PCB抄板
Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?
DAY18: XSS vulnerability
postgresql源码学习(33)—— 事务日志⑨ - 从insert记录看日志写入整体流程
One of the small practical projects - food alliance ordering system
熟悉而陌生的新朋友——IAsyncDisposable
iOS大厂面试查漏补缺
随机推荐
DirectExchange switch simple introduction demo
基于交替迭代法的交直流混合系统潮流计算matlab程序iEEE9节点系统算例
Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
批量翻译软件免费【2022最新版】
2022.07.14_每日一题
[PSQL] SQL基础教程读书笔记(Chapter1-4)
单点登录 思维导图
HighTec 的安装与配置
2022.07.29_Daily Question
codec2 BlockPool:不可读库
Run the NPM will pop up to ask "how are you going to open this file?"
庐山谣寄卢侍御虚舟
Log4net 思维导图
2022.07.12_每日一题
【第四章】详解Feign的实现原理
深度学习通信领域相关经典论文、数据集整理分享
Moment.js common methods
多进程全局变量失效、变量共享问题
双倍数据速率同步动态随机存储器(Double Data Rate Synchronous Dynamic Random Access Memory, DDR SDRAM)- 逻辑描述部分
【科普向】5G核心网架构和关键技术