[抄题]:

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. NumPy 基础用法

    NumPy 是高性能科学计算和数据分析的基础包. 它是 pandas 等其他各种工具的基础. 主要功能: ndarray 一个多维数组结构, 高效且节省空间 无需循环对整组数据进行快速运算的数学函数 ...

  2. C# Restful 启用 Session

    虽然很多人说不建议启用,但我就是想启用. [ServiceContract(SessionMode=SessionMode.Allowed)] public interface IBIService ...

  3. MySQL Execution Plan--IN查询计划(2)

    在MySQL中,IN查找经常出现性能问题,相同SQL在MySQL不同版本中表现不同. 准备测试数据: ## 创建表tb001 CREATE TABLE tb001( id INT unsigned N ...

  4. .htaccess FollowSymlinks影响rewrite功能

    Thinkphp的框架的根目录的.htaccess是这样写的: <IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine ...

  5. Postgresql导出数据报版本不对

    zabbix使用得数据库是Postgresql,最近zabbix4.0版本出来了,准备把zabbix升级,得先把数据库备份,但是一直报错,如下:     查找服务器上是否有10的版本,也一直没找到   ...

  6. Updates were rejected because the tip of your current branch is behind 问题出现解决方案

    提供如下几种方式: 1.使用强制push的方法(多人协作时不可取): git push -u origin master -f 2.push前先将远程repository修改pull下来 git pu ...

  7. 5、在Dreamweaver cc 2017中添加服务器扩展组件

    Adobe DW CC 2015对HTML5.CSS3.jQuery.jQuery UI都有很好的支持,无奈这个版本却未提供开发动态网站所需要的服务器行为面板.数据库面板以及绑定面板等.要添加这个面板 ...

  8. 不常用的vi命令

    vi u 撤回ctrl+r 撤回的撤回 全文替换%s/old/new/g 指定行区间替换12,15s/old/new/g c替换前确认12,15s/old/new/gc 用#代替分隔符,用户关键字有/ ...

  9. git 更新远程分支列表

    git remote update origin --prune git remote update origin -p

  10. problem:浏览器如何区分html超文本和普通文本

    运营同学问:后端返回的一串元素标签,我想在网页中显示的时候,将标签中的内容渲染出来,不希望直接显示标签. 回答:bootstrap加模版组织的网页,模版渲染的数据只能渲染字符串,不能转化富文本. 运营 ...