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;
    }

    }

    /* 如有意见或建议,欢迎评论区留言;如发现代码有误,欢迎批评指正 */
  • 相关阅读:
    深度学习之TensorFlow(一)——基本使用
    64位win10+cuda8.0+vs2013+cuDNN V5下Caffe的编译安装教程并配置matlab2014a 接口
    Win10+vs2012+cuda8.0的安装与配置
    图像处理与matlab实例之图像平滑(一)
    Windows下pycharm使用theano的方法
    Python中的支持向量机SVM的使用(有实例)
    混淆矩阵在Matlab中PRtools模式识别工具箱的应用
    模式识别与机器学习—bagging与boosting
    微服务架构下分布式事务解决方案——阿里GTS
    谈谈分布式事务
  • 原文地址:https://www.cnblogs.com/laydown/p/15685676.html
Copyright © 2011-2022 走看看