[LeetCode] 735. Asteroid Collision
行星碰撞。
题意是给一个数组 asteroids,表示在同一行的行星。对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动)。每一颗行星以相同的速度移动。找出碰撞后剩下的所有行星。
碰撞规则:两个行星相互碰撞,较小的行星会爆炸。如果两颗行星大小相同,则两颗行星都会爆炸。两颗移动方向相同的行星,永远不会发生碰撞。例子,
Example 1:
Input: asteroids = [5, 10, -5] Output: [5, 10] Explanation: The 10 and -5 collide resulting in 10. The 5 and 10 never collide.Example 2:
Input: asteroids = [8, -8] Output: [] Explanation: The 8 and -8 collide exploding each other.Example 3:
Input: asteroids = [10, 2, -5] Output: [10] Explanation: The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.Example 4:
Input: asteroids = [-2, -1, 1, 2] Output: [-2, -1, 1, 2] Explanation: The -2 and -1 are moving left, while the 1 and 2 are moving right. Asteroids moving the same direction never meet, so no asteroids will meet each other.
思路是用stack做。先创建一个空的stack,一开始当stack为空的时候,直接就把数组里面的元素加进去;当stack不为空的时候,做如下判断
1. 先看一下栈顶元素的正负和要塞入的元素的正负。如果栈顶元素为负(往左)或者要塞入的元素为正(往右),说明加入栈的时候不会发生碰撞,直接就加了;
2. 除了第一种情况,其他情况就有可能会发生碰撞了。这时候判断如果栈顶元素 + 塞入元素 < 0说明两者会相向碰撞并且栈顶元素会被损毁,此时要pop出栈顶元素并且i--,看看试图加入栈的元素能否把新的栈顶元素(原来是在i - 1的位置上的元素)再次损毁
3. 如果两者相向碰撞但是速度一样,两者互相抵消,栈顶元素直接pop
时间O(n)
空间O(n)
/**
* @param {number[]} asteroids
* @return {number[]}
*/
var asteroidCollision = function (asteroids) {
const stack = [];
const len = asteroids.length;
for (let i = 0; i < len; i++) {
const cur = asteroids[i];
// if stack is empty, just push
if (!stack.length) {
stack.push(cur);
} else {
// peek the stack top
const pop = stack[stack.length - 1];
if (pop < 0 || cur > 0) {
stack.push(cur);
} else if (pop + cur < 0) {
stack.pop();
i--;
} else if (pop + cur === 0) {
stack.pop();
}
}
}
return stack;
};
[LeetCode] 735. Asteroid Collision的更多相关文章
- 【LeetCode】735. Asteroid Collision 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- 735. Asteroid Collision彗星相撞后的消失数组
[抄题]: We are given an array asteroids of integers representing asteroids in a row. For each asteroid ...
- leeetcode 735. Asteroid Collision
We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...
- [LeetCode] Asteroid Collision 行星碰撞
We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...
- [Swift]LeetCode735. 行星碰撞 | Asteroid Collision
We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...
- LeetCode——735.行星碰撞
给定一个整数数组 asteroids,表示在同一行的行星. 对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动).每一颗行星以相同的速度移动. 找出 ...
- Asteroid Collision
We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...
- Java实现 LeetCode 735 行星碰撞(栈)
735. 行星碰撞 给定一个整数数组 asteroids,表示在同一行的行星. 对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动).每一颗行星以相 ...
- 小行星碰撞 Asteroid Collision
2018-08-07 11:12:01 问题描述: 问题求解: 使用一个链表模拟栈,最后的状态一定是左侧全部是负值,表示的是向左飞行,右侧的全部是正值,表示的是向右飞行. 遍历整个数组,对于每个读到的 ...
随机推荐
- Go操作Elasticsearch
文章转自 Elasticsearch Elasticsearch 下载 https://www.elastic.co/cn/start 运行 解压后cd到解压目录 ./bin/elasticsea ...
- Custom LED Keychain, Small And Surefire Gifts
The LED Keychain makes it easy for people to carry their keys with them and carry them with th ...
- HTML5学习(5)实体字符
HTML Entity 实体字符通常用于在页面中显示一些特殊符号. 书写方式: 1. &单词; 2. &#数字; 常用实体字符: < < litter than &g ...
- active Directory域服务安装配置
1.在Windows功能启用 2.安装一直下一步即可, 添加用户 添加域管理员 将普通用户添加到Domain Admins里
- Yum与list结合
我用两台Linux LinuxA IP:192.168.10.101 LinuxB IP:192.168.10.102 首先我们在LinuxA上挂载光驱和安装FTP服务器 然后安装 ...
- AcWing 872. 最大公约数
#include <iostream> #include <algorithm> using namespace std; //辗转相除法 //a和b的最大公约数 = b和(a ...
- 09day 命令提示符优化及yum优化
export PS1='\[\e[32;1m\][\u@\h \W]\$ \[\e[0m\]' 设置颜色 内容 结束 export PS1='\[\e[30;1m\][\u@\h \W]\$ \[\e ...
- 委托与事件--delegate&&event
委托 访问修饰符 delegate 返回值 委托名(参数); public delegate void NoReturnNoPara(); public void NoReturnNoParaMeth ...
- pwnable.kr-balckjack-Writeup
MarkdownPad Document *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...
- C++记录(一)
1 extern 符表示该变量不是当前作用域定义的,用于声明. 如extern i;表示i不是当前作用域里的,是其他某个include的cpp文件里的变量. 2 int *p=0;相当于初始化p为空指 ...