Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

行星碰撞 #251

Open
louzhedong opened this issue Apr 30, 2021 · 0 comments
Open

行星碰撞 #251

louzhedong opened this issue Apr 30, 2021 · 0 comments

Comments

@louzhedong
Copy link
Owner

习题

给定一个整数数组 asteroids,表示在同一行的行星。

对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动)。每一颗行星以相同的速度移动。

找出碰撞后剩下的所有行星。碰撞规则:两个行星相互碰撞,较小的行星会爆炸。如果两颗行星大小相同,则两颗行星都会爆炸。两颗移动方向相同的行星,永远不会发生碰撞。

示例 1:

输入:asteroids = [5,10,-5]
输出:[5,10]
解释:10 和 -5 碰撞后只剩下 10 。 5 和 10 永远不会发生碰撞。
示例 2:

输入:asteroids = [8,-8]
输出:[]
解释:8 和 -8 碰撞后,两者都发生爆炸。
示例 3:

输入:asteroids = [10,2,-5]
输出:[10]
解释:2 和 -5 发生碰撞后剩下 -5 。10 和 -5 发生碰撞后剩下 10 。
示例 4:

输入:asteroids = [-2,-1,1,2]
输出:[-2,-1,1,2]
解释:-2 和 -1 向左移动,而 1 和 2 向右移动。 由于移动方向相同的行星不会发生碰撞,所以最终没有行星发生碰撞。

提示:

2 <= asteroids.length <= 104
-1000 <= asteroids[i] <= 1000
asteroids[i] != 0

思路

对于每个行星,不确定它和它之前的几个元素会发生碰撞,所以采用栈会是一个比较好的选择,每次比较栈顶元素和当前元素

解答

/**
 * @param {number[]} asteroids
 * @return {number[]}
 */
var asteroidCollision = function (asteroids) {
    var stack = [],
        i = 0,
        length = asteroids.length;
    for (; i < length; i++) {
        var current = asteroids[i];
        if (stack.length == 0) {
            stack.push(current);
        } else {
            while (current < 0 && stack[stack.length - 1] * current < 0) {
                var stackTop = stack[stack.length - 1];
                if (stackTop + current == 0) {
                    stack.pop();
                    current = undefined;
                    break;
                }
                if (Math.abs(stackTop) > Math.abs(current)) {
                    current = stackTop;
                }
                stack.pop();
            }
            if (current) {
                stack.push(current);
            }
        }
    }
    return stack;
};

go

func asteroidCollision(asteroids []int) []int {
    var stack []int = make([]int, 0)
    
    for i := 0; i < len(asteroids); i++ {
        current := asteroids[i]
        disappearFlag := false
        if len(stack) == 0 {
            stack = append(stack, current)
        } else {
            for current < 0 && len(stack) > 0 && stack[len(stack) - 1] * current < 0 {
                stackTop := stack[len(stack) - 1]
                if stackTop + current == 0 {
                    stack = stack[: len(stack) - 1]
                    disappearFlag = true
                    break
                }
                if stackTop > 0 && (stackTop + current > 0) {
                    current = stackTop
                }
                if stackTop < 0 && (stackTop + current < 0) {
                    current = stackTop
                }
                stack = stack[: len(stack) - 1]
            }
            if disappearFlag == false {
                stack = append(stack, current)
            }
        }
    }
    return stack
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant