//定义栈对象,让数组里的数据进栈,如果是正数,就不判断,如果是负数进行判断
public static int[] asteroidCollision(int[] asteroids) {
Stack <Integer> stack =new Stack<Integer>();Stack <Integer> stacks=new Stack<Integer>();
for(int i=0;i<asteroids.length;i++) {
if(!stack.isEmpty()) {
while(!stack.isEmpty()) {
//当栈内只有一个数,且为正,小于数组中的数,就出栈,数组中的数字进栈
if(asteroids[i]<0&&stack.peek()>0&&stack.peek()<Math.abs(asteroids[i])&&stack.size()==1) {stack.pop();
stack.push(asteroids[i]);
break;
}
//当栈内不只有一个数,就做出栈处理,然后继续比较
else if(asteroids[i]<0&&stack.peek()>0&&stack.peek()<Math.abs(asteroids[i])) {stack.pop();
}
//当栈内的数大于数组的数,就跳出循环
else if(asteroids[i]<0&&stack.peek()>0&&stack.peek()>Math.abs(asteroids[i])) {break;
}
//当栈内数与数组数相同,就做出栈处理
else if(asteroids[i]<0&&stack.peek()>0&&stack.peek()==Math.abs(asteroids[i])) {stack.pop();
break;
}
//当栈内没有数字就做入栈处理
else {stack.push(asteroids[i]);
break;
}
}
}
else {
stack.push(asteroids[i]);
}
}
int [] res= new int[stack.size()];
int i=0;
while(!stack.isEmpty()) {
stacks.push(stack.pop());
}
while(!stacks.isEmpty()) {
res[i]=stacks.pop();
i++;
}
return res;
}