zoukankan      html  css  js  c++  java
  • 行星碰撞

    import java.util.Arrays;

    /**
    * 行星碰撞问题
    * We are given an array asteroids of integers representing asteroids in a row.
    * <p>
    * 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.
    * <p>
    * 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.
    * <p>
    * Example 1:
    * <p>
    * Input: asteroids = [5,10,-5]
    * Output: [5,10]
    * Explanation: The 10 and -5 collide resulting in 10. The 5 and 10 never collide.
    * <p>
    * Example 2:
    * <p>
    * Input: asteroids = [8,-8]
    * Output: []
    * Explanation: The 8 and -8 collide exploding each other.
    * <p>
    * Example 3:
    * <p>
    * Input: asteroids = [10,2,-5]
    * Output: [10]
    * Explanation: The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.
    * <p>
    * Example 4:
    * <p>
    * 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.
    * <p>
    * Constraints:
    * <p>
    * 2 <= asteroids.length <= 10^4
    * -1000 <= asteroids[i] <= 1000
    * asteroids[i] != 0
    */
    public class _735_AsteroidCollision {

    public static void main(String[] args) {
    _735_AsteroidCollision asteroidCollision = new _735_AsteroidCollision();
    int[] arr = new int[]{5, 10, -5};
    System.out.println(Arrays.toString(asteroidCollision.asteroidCollision(arr)));
    arr = new int[]{8, -8};
    System.out.println(Arrays.toString(asteroidCollision.asteroidCollision(arr)));
    arr = new int[]{10, 2, -5};
    System.out.println(Arrays.toString(asteroidCollision.asteroidCollision(arr)));
    arr = new int[]{-2, -1, 1, 2};
    System.out.println(Arrays.toString(asteroidCollision.asteroidCollision(arr)));
    }

    public int[] asteroidCollision(int[] asteroids) {
    // 新分配数组从0开始赋值
    int[] arr = new int[asteroids.length];
    int index = 0;
    // 当前数组是否有修改
    boolean update = false;
    for (int i = 0; i < asteroids.length; i++) {
    int i1 = asteroids[i];
    if (i + 1 == asteroids.length) { // 只剩一个行星
    arr[index++] = i1;
    } else {
    int i2 = asteroids[i + 1];
    if (i1 > 0 && i1 == -i2) { // 两行星对撞,相等则消失
    i++;
    update = true;
    } else if (i1 > 0 && i2 < 0) { // 两行星对撞,不相等取大的
    arr[index++] = i1 + i2 > 0 ? i1 : i2;
    i++;
    update = true;
    } else { // 两行星不会对撞
    arr[index++] = i1;
    }
    }
    }
    return update ?
    // 有修改返回过滤之后的值
    asteroidCollision(Arrays.copyOf(arr, index)) :
    // 无修改直接返回对应数组
    asteroids;
    }

    }

    /* 如有意见或建议,欢迎评论区留言;如发现代码有误,欢迎批评指正 */
  • 相关阅读:
    python 获取qq群成员列表数据
    selenium定位伪类元素::before
    flask 重写HTTPException类并自定义异常信息
    flask 工厂模式与celery结合
    重写flask_sqlalchemy的BaseQuery下面的filter_by方法
    Pytest系列
    Pytest系列- 多重校验插件之pytest-assume的详细使用
    mysql 主主+ Keepalived 高可用
    redis 主从配置,主从切换
    FastDFS 学习笔记
  • 原文地址:https://www.cnblogs.com/laydown/p/15685676.html
Copyright © 2011-2022 走看看