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. DataGrid控件的列

    四种列(局限性较大)https://www.cnblogs.com/lonelyxmas/p/9442604.html 更强大的模板列(如控件居中等)https://www.cnblogs.com/l ...

  2. hdu 5792 World is Exploding 树状数组+离散化+容斥

    World is Exploding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  3. zabbix服务端接收的数据类型,便于编写脚本向服务端提交数据

    1.数据类型1:zabbix_agent执行脚本提交字典 UserParameter=tcp_port_listen,/usr/local/zabbix/share/script/get_game_p ...

  4. Jmeter(六)关联之XPath提取器

    如果请求返回的消息为xml或html格式的,可以用XPath提取器来提取需要的数据 以http://www.weather.com.cn/为例: 先新建一个HTTP请求GetCityURL,获取城市天 ...

  5. StackTraceElement 源码阅读

    StackTraceElement 属性说明 /** * 每个 StackTraceElement 对象代表一个独立的栈帧,所有栈帧的顶部是一个方法调用 * @since 1.4 * @author ...

  6. LC 406. Queue Reconstruction by Height

    Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...

  7. 五十四:WTForms表单验证之自定义表单验证器

    如果想要对表单中的某个字段进行自定义验证,则需要对这个字段进行单独的验证1.定义一个方法,命名规则为:validate_字段名(self, filed)2.在方法中,使用filed.data获取字段的 ...

  8. 关于springmvc的包含list提交的格式

    <%-- Created by IntelliJ IDEA. User: jh Date: 2017/7/12 Time: 14:31 To change this template use F ...

  9. zabbix(2)使用指南

    一.邮件报警(一个客户端安装server,agent) 管理->报警媒介类型->email 管理->用户->Admin->报警媒介 配置->动作->Repor ...

  10. POJO是什么,javabean是什么,以及POJO与javabean的区别

    POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称.使用POJO名称是为了避免和EJB混淆起来, 而 ...