行星碰撞。

题意是给一个数组 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的更多相关文章

  1. 【LeetCode】735. Asteroid Collision 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...

  2. 735. Asteroid Collision彗星相撞后的消失数组

    [抄题]: We are given an array asteroids of integers representing asteroids in a row. For each asteroid ...

  3. leeetcode 735. Asteroid Collision

    We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...

  4. [LeetCode] Asteroid Collision 行星碰撞

    We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...

  5. [Swift]LeetCode735. 行星碰撞 | Asteroid Collision

    We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...

  6. LeetCode——735.行星碰撞

    给定一个整数数组 asteroids,表示在同一行的行星. 对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动).每一颗行星以相同的速度移动. 找出 ...

  7. Asteroid Collision

    We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...

  8. Java实现 LeetCode 735 行星碰撞(栈)

    735. 行星碰撞 给定一个整数数组 asteroids,表示在同一行的行星. 对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动).每一颗行星以相 ...

  9. 小行星碰撞 Asteroid Collision

    2018-08-07 11:12:01 问题描述: 问题求解: 使用一个链表模拟栈,最后的状态一定是左侧全部是负值,表示的是向左飞行,右侧的全部是正值,表示的是向右飞行. 遍历整个数组,对于每个读到的 ...

随机推荐

  1. cc.Sprite 与 ccui.ImageView 改变图片

    sprite.setTexture(fileName); imageView.loadTexture(fileName);

  2. 在Linux系统上安装配置ant环境

    1.从官网http://ant.apache.org/bindownload.cgi下载tar.gz版ant到本地电脑上 2.通过WinSCP工具将本地电脑上的ant压缩包上传至Linux服务器的/u ...

  3. sqlserver表中导入大批量数据

    背景: 想要往sqlserver数据库中导入大批量数据,使得数据库的备份文件大于几个G. 注意: 导入的数据不能太规范,数据表最好不一致,否则会自动压缩. 解决办法: 1)通过excel导入,可以参考 ...

  4. LED Decorative Light Manufacturer Introduction: LED Metal Table Light

    Nowadays, when many people choose the desk light, they are worried that it will not be used for a lo ...

  5. LED Magic Light - How Does The LED Light Change Color?

    The    LED Magic Light    states that the color-changing LED is not an LED in the package, but three ...

  6. Codeforces 1207C Gas Pipeline (dp)

    题目链接:http://codeforces.com/problemset/problem/1207/C 题目大意是给一条道路修管道,相隔一个单位的管道有两个柱子支撑,管道柱子高度可以是1可以是2,道 ...

  7. POJ 3177 Redundant Paths (tarjan边双连通分量)

    题目连接:http://poj.org/problem?id=3177 题目大意是给定一些牧场,牧场和牧场之间可能存在道路相连,要求从一个牧场到另一个牧场要有至少两条以上不同的路径,且路径的每条pat ...

  8. docker互联机制实现便捷互访

    何为容器互联 & 为何需要容器互联 容器的互联是一种让多个容器中应用进行快速交互的方式,它会在源和接收容器之间创建连接关系,接收容器可以通过容器名快速访问到源容器,而不用指定具体的 ip 地址 ...

  9. PHP 源码 —— is_array 函数源码分析

    is_array 函数源码分析 本文首发于 https://github.com/suhanyujie/learn-computer/blob/master/src/function/array/is ...

  10. Type Java类型

    参考:https://blog.csdn.net/a327369238/article/details/52621043 Type —— Java类型 Type是一个空接口,所有类型的公共接口(父接口 ...