[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么改数组,忘了:新建一个linked list之后动态添加即可

[英文数据结构或算法,为什么不用别的数据结构或算法]:

s.stream().mapToInt(i->i).toArray(); integer对象转为数字流,然后再转数组。

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 不理解:减少一位需要i--,绝对值相等时+1-1,不用i--

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

对linkedlist可以用polllast getlast的方法

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public int[] asteroidCollision(int[] asteroids) {
//initialization: array
LinkedList<Integer> list = new LinkedList<>(); //for loop: add to array if positive, or compare and poll
for (int i = 0; i < asteroids.length; i++) {
if (asteroids[i] > 0 || list.size() == 0 || list.getLast() < 0) {
list.add(asteroids[i]);
}else {
//if bigger or equal, i--
if (list.getLast() <= -asteroids[i]) {
//if bigger, romove only the last
if (list.pollLast() < -asteroids[i]) {
i--;
}
}
}
} //return .stream.to.toarray
return list.stream().mapToInt(i -> i).toArray();
}
}

735. Asteroid Collision彗星相撞后的消失数组的更多相关文章

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

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

  2. leeetcode 735. Asteroid Collision

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

  3. [LeetCode] 735. Asteroid Collision

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

  4. [LeetCode] Asteroid Collision 行星碰撞

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

  5. js弹框3秒后自动消失

    开发中有时候会需要最出弹框后,过几秒自动消失的效果,下面给大家分享一下我自己做的一个小案例. 案例中的弹框使用的是bootstrap里面的模态框,实现自动消失则用的是js中的setInterval方法 ...

  6. hadoop namenode又一次格式化以后hbase的hmaster进程启动后立即消失

    hadoop的 namenode又一次格式化以后.重新启动hbase.发现它的hmaster进程启动后立即消失,查看一大堆日志,最后在zookeeper的日志里发现例如以下问题 Unable to r ...

  7. jq弹框 (1)内容自适应宽度 2(内容框显示,几秒后自动消失)

      <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&q ...

  8. Axure实现提示文本单击显示后自动消失的效果

    Axure实现提示文本单击显示后自动消失的效果 方法/步骤     如图所示,框出的部分为提示文本(已经命名为tooltip),希望达到的效果是默认加载时不显示,点击帮助图标后显示,且2秒后自动消失. ...

  9. Bootstrap告警框(alert)实现弹出效果和短暂消失后上浮消失

    最近用到bootstrap的告警框时发现只有html的说明,就自己写了一个弹出告警框和弹出短暂显示后上浮消失的告警框. 直接上JS代码了,可以copy过去直接用(使用bootstrap的UI框架的情况 ...

随机推荐

  1. java 中Math 的常用方法

    public class Demo{ public static void main(String args[]){ /** *Math.sqrt()//计算平方根 *Math.cbrt()//计算立 ...

  2. C#编码、解码与ASP.NET编码解码对应函数

    JavaScript函数分别为:encodeURIComponent/decodeURIComponent C#对应的函数分别为:Uri.EscapeUriString/Uri.EscapeDataS ...

  3. PythonStudy——Python 内存池机制 (Memory pool mechanism) Pymalloc

    Python是如何进行内存管理-内存池机制 Pymalloc Python引用了一个内存池(memory pool)机制,即Pymalloc机制(malloc:n.分配内存),用于对小块内存的申请和释 ...

  4. The problems when using a new ubuntu 18.04

    how to install dual systems (windows & ubuntu) Donwloading the ubuntu from web. Using refu to cr ...

  5. 代码本色 用编程模拟自然系统 (Daniel Shiffman 著)

    https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.7/p5.js http://www.box2d.org http://www.jbox2d.org ...

  6. oracle-ords

    oracle rest data service ORDS Perforce    adv. 一定,必须:必然地pagination    n. 标记页数:页码,分页Online documentat ...

  7. keepalive实现MGR的自动切换(二)

    10.0.0.7 lemon 10.0.0.8 lemon2 10.0.0.9 lemon3   程序代码里只需写一个VIP连接数据库即可,后面是连接在哪一台通过,keepalived的在服务端实现: ...

  8. php 字符串固定长度,不够补充其他字符串

    <?php $input = '456'; $var= str_pad($input,5,10,STR_PAD_LEFT); w3school手冊:http://www.w3school.com ...

  9. 二十、springcloud(六)配置中心服务化和高可用

    1.问题描述 前一篇,spring-cloud-houge-provider(称之为客户端)直接从spring-cloud-houge-config(称之为服务端)读取配置,客户端和服务端的耦合性太高 ...

  10. SVN简单使用

    如果是window操作系统,默认安装.右键菜单就会有显示SVN 如果已经配置好SVN,直接确定既可以检出. 如果没有配置,那么会显示下面的验证: 输入用户名和密码即可