-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain37.java
More file actions
25 lines (23 loc) · 742 Bytes
/
Main37.java
File metadata and controls
25 lines (23 loc) · 742 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package JZOfferTuJi;
import java.util.Stack;
public class Main37 {
public int[] asteroidCollision(int[] asteroids) {
Stack<Integer> stack = new Stack<>();
int index = 0, length = asteroids.length;
while (index < length){
if(stack.isEmpty() || stack.peek() < 0 || asteroids[index] > 0){
stack.push(asteroids[index]);
}else if(stack.peek() <= -asteroids[index]){
if(stack.pop() < -asteroids[index]){
continue;
}
}
index++;
}
int[] ret = new int[stack.size()];
for(int i = ret.length - 1; i >= 0; i--){
ret[i] = stack.pop();
}
return ret;
}
}