2018-08-07 11:12:01

问题描述:

问题求解:

使用一个链表模拟栈,最后的状态一定是左侧全部是负值,表示的是向左飞行,右侧的全部是正值,表示的是向右飞行。

遍历整个数组,对于每个读到的数字,如果是正值则直接加入链表中,如果是负值,则需要判断链表中末尾的数字如果也是负值,则表示目前链表中全部向左飞行,则加入,如果说此时链表中最后的数字为正值,则表示会相撞,需要进行比较判断。

这个题目的解法也给出了如果将Colleaction转化为int[],可以使用colleaction.stream().mapToInt(i -> i).toArray()来进行转换。

    public int[] asteroidCollision(int[] asteroids) {
LinkedList<Integer> res = new LinkedList<>();
for (int i = 0; i < asteroids.length; i++) {
if (asteroids[i] > 0 || res.size() == 0 || res.getLast() < 0) {
res.add(asteroids[i]);
}
else if (res.getLast() <= -asteroids[i]) {
if (res.pollLast() < -asteroids[i]) i--;
}
}
return res.stream().mapToInt(i -> i).toArray();
}

小行星碰撞 Asteroid Collision的更多相关文章

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

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

  2. 密码学系列之:碰撞抵御和碰撞攻击collision attack

    密码学系列之:碰撞抵御和碰撞攻击collision attack 简介 hash是密码学和平时的程序中经常会用到的一个功能,如果hash算法设计的不好,会产生hash碰撞,甚至产生碰撞攻击. 今天和大 ...

  3. [LeetCode] Asteroid Collision 行星碰撞

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

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

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

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

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

  6. leeetcode 735. Asteroid Collision

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

  7. Asteroid Collision

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

  8. [LeetCode] 735. Asteroid Collision

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

  9. Swift LeetCode 目录 | Catalog

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

随机推荐

  1. linux lvs

  2. 002-ubuntu安装

    一.安装了ubuntu desktop版本后: 1.进行桥接联网. 2.运行更新:#sudo apt-get update. 3.安装net-tools网络工具包:#sudo apt install ...

  3. Mybatis—三剑客之generator使用方法

    三剑客之generator主要用于自动生成POJO实体类   准备素材: mybatis-generator-core-1.3.2.jar     mysql-connector-java-5.1.2 ...

  4. Shell篇(三)TC Shell

    Shell脚本的首行一般写为"#!+路径"来告诉系统,以路径所指定的程序来解释此脚本. 可以写为 #! /bin/tcsh -f (-f表示快速启动,不启动~/.tcshrc) S ...

  5. VS2010/MFC编程入门之三十六(工具栏:工具栏资源及CToolBar类)

    上一节中鸡啄米讲了菜单及CMenu类的使用,这一节讲与菜单有密切联系的工具栏. 工具栏简介 工具栏一般位于主框架窗口的上部,菜单栏的下方,由一些带图片的按钮组成.当用户用鼠标单击工具栏上某个按钮时,程 ...

  6. spring mvc interceptors

    <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <mvc:e ...

  7. Linux服务器配置---ntp

    配置ntp     ntp就是网络时间同步的服务,时间的准确性非常重要,很多数据在记录时都要知道准确的时间.网上有很多站点,一般国内会设置匹配中科院国家授时中心的时间. 1.安装ntp软件 [root ...

  8. [转载]论asp.net out、ref、return

      论asp.net out.ref.return ref(引用类型) ref引用类型进出都必须赋值,赋值后会改变类型原来的指针.   out(值类型) out值类型进可以不赋值,出必须赋值.   r ...

  9. Matchvs 使用记录

    Matchvs Matchvs视频教程. https://doc.matchvs.com/VideoTutorials/videogs matchvs下载资源. http://www.matchvs. ...

  10. 使用CSP防止XSS攻击

    转载自阮一峰博客:http://www.ruanyifeng.com/blog/2016/09/csp.html 跨域脚本攻击 XSS 是最常见.危害最大的网页安全漏洞. 为了防止它们,要采取很多编程 ...