We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 68d3c7b + 2cb5abd commit 12e57ecCopy full SHA for 12e57ec
java/0735-asteroid-collision.java
@@ -0,0 +1,34 @@
1
+class Solution {
2
+ public int[] asteroidCollision(int[] asteroids) {
3
+ Stack<Integer> stack = new Stack<>();
4
+ int index = 0;
5
+
6
+ while(index < asteroids.length) {
7
+ int a = asteroids[index];
8
+ if(stack.isEmpty()) {
9
+ stack.push(a);
10
+ index++;
11
+ } else {
12
+ if(stack.peek() > 0 && a < 0) {
13
+ if(Math.abs(stack.peek()) > Math.abs(a)) {
14
15
+ } else if(Math.abs(stack.peek()) < Math.abs(a)) {
16
+ stack.pop();
17
18
19
20
+ }
21
22
23
24
25
26
27
28
+ int[] res = new int[stack.size()];
29
+ for(int i=res.length-1; i>=0 ;i--) {
30
+ res[i] = stack.pop();
31
32
+ return res;
33
34
+}
0 commit comments