zoukankan      html  css  js  c++  java
  • LeetCode 735. Asteroid Collision

    原题链接在这里:https://leetcode.com/problems/asteroid-collision/

    题目:

    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.

    Note:

    • The length of asteroids will be at most 10000.
    • Each asteroid will be a non-zero integer in the range [-1000, 1000]..

    题解:

    Have a queue, when it is positive number ,add to the back.

    When it is negative, poll from the last if last number is positive but smaller than -num.

    If queue is empty or last number is negative, then add num. 

    Else if last number == -num, poll it out.

    The nums in queue is the result.

    Time Complexity: O(n). n = asteroids.length.

    Space: O(n).

    AC Java:

     1 class Solution {
     2     public int[] asteroidCollision(int[] asteroids) {
     3         if(asteroids == null || asteroids.length == 0){
     4             return asteroids;
     5         }
     6         
     7         LinkedList<Integer> que = new LinkedList<>();
     8         for(int num : asteroids){
     9             if(num > 0){
    10                 que.add(num);
    11             }else if(num < 0){
    12                 while(!que.isEmpty() && que.peekLast() > 0 && que.peekLast() < -num){
    13                     que.pollLast();
    14                 }
    15                 
    16                 if(que.isEmpty() || que.peekLast() < 0){
    17                     que.add(num);
    18                 }else if(que.peekLast() == -num){
    19                     que.pollLast();
    20                 }
    21             }
    22         }
    23         
    24         int [] arr = new int[que.size()];
    25         for(int i = 0; i < arr.length; i++){
    26             arr[i] = que.pollFirst();
    27         }
    28         
    29         return arr;
    30     }
    31 }
  • 相关阅读:
    让我偷偷的告诉你:运维加薪的杀手锏是啥?
    网站页面优化必然趋势—WebP 图片!
    如何监控 Tomcat?Zabbix 与 Cloud Insight 对比
    网页增重不可控?试试 OneAPM Cloud Test
    提高 ASP.NET Web 应用性能的 24 种方法和技巧
    OneAlert 入门(三)——事件分析
    Cloud Insight 客户案例-晨芯时代科技有限公司
    Java开发快速上手
    微信小程序快速开发上手
    微信小程序快速开发上手
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12204991.html
Copyright © 2011-2022 走看看