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:

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.
 class Solution {
public int[] asteroidCollision(int[] a) {
LinkedList<Integer> s = new LinkedList<>();
for (int i = ; i < a.length; i++) {
if (a[i] > || s.isEmpty() || s.getLast() < ) {
s.add(a[i]);
} else if (s.getLast() == -a[i]) {
s.pollLast();
} else if (s.getLast() < -a[i]) {
s.pollLast();
i--; // very clever idea
}
}
return s.stream().mapToInt(i -> i).toArray();
}
}

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. CSS的水平居中和垂直居中

    水平居中如果不太熟悉盒子模型的话属实不太好理解,其实就是控制其他属性来让border之内的内容被控制在父容器中间就行了,最经典的就是使用{margin: 0  auto}了,控制其上下外边框为0,左右 ...

  2. MessagePack Java 0.6.X List, Map 对象的序列化和反序列化

    为了序列化原生的容器对象例如  List 和 Map 对象,你必须使用 Template. Template 对象是 serializer 和 deserializer 的配对.例如,为了序列化一个  ...

  3. AtCoder AGC004F Namori (图论)

    题目链接 https://atcoder.jp/contests/agc004/tasks/agc004_f 题解 神仙题.. 首先考虑树的情况,树是二分图,因此假设我们对二分图进行黑白染色,那么操作 ...

  4. 树状数组(BIT)

    树状数组 树状数组是在线段树的结构上改造而来数据结构,主要用于完成: 给定一个初始值全为0的数列 ①给定i,计算返回a1+a2+--+ai的值 ②给定i和x,执行ai+=x BIT的求和 ll sum ...

  5. phpcms9 从注入点入手和 从前台getshell

    弄了3天了  这个点 总结一下这三天的坑吧 0X01 注入点入手 /index.php?m=wap&c=index&a=init&siteid=1 获取cookie 传给 us ...

  6. DB 分库分表(3):关于使用框架还是自主开发以及 sharding 实现层面的考量

    当团队对系统业务和数据库进行了细致的梳理,确定了切分方案后,接下来的问题就是如何去实现切分方案了,目前在sharding方面有不少的开源框架和产品可供参考,同时很多团队也会选择自主开发实现,而不管是选 ...

  7. Java 注解指导手册(上)

      编者的话:注解是java的一个主要特性且每个java开发者都应该知道如何使用它.   我们已经在Java Code Geeks提供了丰富的教程, 如Creating Your Own Java A ...

  8. 开源!js实现微信/QQ直接跳转到支付宝APP打开口令领红包!附:demo

    最近支付宝的领红包可真是刷爆了各个微信群啊,满群都是支付宝口令. 可是这样推广可不是办法,又要复制又要打开支付宝又要点领取,太麻烦了. 于是乎,提出了一个疑问!是否可以在微信里面点一个链接然后直接打开 ...

  9. 20165213 Exp9 Web安全基础

    Exp9 Web安全基础 一.基础性问答 (1)SQL注入攻击原理,如何防御 原理:SQL注入即是指web应用程序对用户输入数据的合法性没有判断,攻击者可以在web应用程序中事先定义好的查询语句的结尾 ...

  10. python 学习笔记(二):为元组的每个元素命名,提高程序的可读性

    在程序中有些数据为固定格式时,即字段数量确定.字段位置顺序确定不变,我们就可以用元组来储存.使用元组的优势是储存空间很小,访问速度也很快.如下代码对每个学生信息用元组来表示: # ('Jim', 16 ...