We are given an array asteroids of integers representing asteroids in a row.

For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.

Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.

Example 1:

  1. Input:
  2. asteroids = [5, 10, -5]
  3. Output: [5, 10]
  4. Explanation:
  5. The 10 and -5 collide resulting in 10. The 5 and 10 never collide. 

Example 2:

  1. Input:
  2. asteroids = [8, -8]
  3. Output: []
  4. Explanation:
  5. The 8 and -8 collide exploding each other.

Example 3:

  1. Input:
  2. asteroids = [10, 2, -5]
  3. Output: [10]
  4. Explanation:
  5. The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.

Example 4:

  1. Input:
  2. asteroids = [-2, -1, 1, 2]
  3. Output: [-2, -1, 1, 2]
  4. Explanation:
  5. The -2 and -1 are moving left, while the 1 and 2 are moving right.
  6. Asteroids moving the same direction never meet, so no asteroids will meet each other.
  7.  
  1. class Solution {
  2. public int[] asteroidCollision(int[] a) {
  3. LinkedList<Integer> s = new LinkedList<>();
  4. for (int i = ; i < a.length; i++) {
  5. if (a[i] > || s.isEmpty() || s.getLast() < ) {
  6. s.add(a[i]);
  7. } else if (s.getLast() == -a[i]) {
  8. s.pollLast();
  9. } else if (s.getLast() < -a[i]) {
  10. s.pollLast();
  11. i--; // very clever idea
  12. }
  13. }
  14. return s.stream().mapToInt(i -> i).toArray();
  15. }
  16. }

Asteroid Collision的更多相关文章

  1. [LeetCode] Asteroid Collision 行星碰撞

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

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

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

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

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

  4. leeetcode 735. Asteroid Collision

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

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

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

  6. 小行星碰撞 Asteroid Collision

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

  7. [LeetCode] 735. Asteroid Collision

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

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. 医院设置x

    医院设置x   题目描述 Description 设有一棵二叉树,如下图 其中,圈中数字表示结点居民的人口.圈边上数字表示结点编号,.现在要求在某个结点上建立一个医院,使所有居民所走的路程之和为最小, ...

  2. CUDA-F-1-1-异构计算-CUDA

    开篇废话 成熟与智慧其实和年龄相关,但绝不是完全由年龄决定,少年老成的人肯定是存在的,不是长得老,而是心态成熟,当然大多数老年人其实有些事情思考起来还是老原则,所以他们有时候做事没那么周到,所以一个人 ...

  3. CF762F Tree nesting

    题目连接 问题分析 可以给小树钦定一个根, \(Dp[i][j]\) 表示大树上的点 \(i\) 对应到小树上的点 \(j\) 的可能的方案数.然后每一步转移都是一个状压DP(将小树是否被匹配状压,然 ...

  4. django CBV模式源码执行过程

    在说CBV模式之前,先看下FBV的url配置方式: urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^xxx/', login), ur ...

  5. leetcode题目11.盛最多水的容器(中等)

    题目描述: 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其 ...

  6. 2018-2019-2 20175215 实验三《敏捷开发与XP实践》实验报告

    一.实验内容与步骤 1.安装.使用alibaba 插件规范代码 在IDEA的setting中找到plugins并搜索alibaba,点击install进行安装 重启IDEA后,在代码中右击点击编码规约 ...

  7. Nginx-rtmp点播之业务流程分析

    1. 点播的播放流程分析 1.1 ngx_rtmp_cycle 在握手结束后,即进入该函数中做进一步处理. void ngx_rtmp_cycle(ngx_rtmp_session_t *s) { n ...

  8. Python中函数的使用

    函数让代码的编写,阅读,测试和修改都变得更容易,提高代码的复用性,python中使用def关键字定义函数 如下代码在python3.7.3的Genay开发工具中编写测试通过. 一.简单函数定义及调用 ...

  9. koa 基础(十)原生node.js 在 koa 中获取表单提交的数据

    1.app.js // 引入模块 const Koa = require('koa'); const router = require('koa-router')(); /*引入是实例化路由 推荐*/ ...

  10. 访问H2数据库的SpringBoot工程

    JDK:1.8.0_212 IDE:STS4(Spring Tool Suit4 Version: 4.3.2.RELEASE) 工程下载:https://files.cnblogs.com/file ...