思路:借助栈结构
public int[] asteroidCollision(int[] asteroids) {
List<Integer> res = new ArrayList<>();
LinkedList<Integer> stack = new Stack<>();
for (int i = 0; i < asteroids.length; i++) {
if (asteroids[i] > 0) {
stack.push(asteroids[i]);
} else {
int size = Math.abs(asteroids[i]);
while (!stack.isEmpty() && size > stack.peek())
stack.pop();
if (!stack.isEmpty()) {
if (stack.peek() == size) {
stack.pop();
}
} else {
res.add(asteroids[i]);
}
}
}
res.addAll(stack);
return res.stream().mapToInt(i -> i).toArray();
}