Skip to content

Commit 9061482

Browse files
committed
Create 0735-asteroid-collision.swift
1 parent e650410 commit 9061482

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
func asteroidCollision(_ asteroids: [Int]) -> [Int] {
3+
var stack = [Int]()
4+
5+
for var a in asteroids {
6+
while !stack.isEmpty && a < 0 && stack.last! > 0 {
7+
let diff = a + stack.last!
8+
if diff < 0 {
9+
stack.popLast()
10+
} else if diff > 0 {
11+
a = 0
12+
} else {
13+
a = 0
14+
stack.popLast()
15+
}
16+
}
17+
18+
if a != 0 {
19+
stack.append(a)
20+
}
21+
}
22+
23+
return stack
24+
}
25+
}

0 commit comments

Comments
 (0)