Skip to content

Instantly share code, notes, and snippets.

@phpcyy
Last active July 10, 2020 07:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save phpcyy/283bc10742b89e9daa908fc2c1270820 to your computer and use it in GitHub Desktop.
func asteroidCollision(asteroids []int) []int {
if len(asteroids) <= 1 {
return asteroids
}
result := make([]int, 0)
for j := 0; j < len(asteroids); {
if len(result) == 0 {
result = append(result, asteroids[j])
j++
continue
}
if result[len(result)-1] < 0 || asteroids[j] > 0 {
result = append(result, asteroids[j])
j++
continue
}
if result[len(result)-1]+asteroids[j] > 0 {
j++
continue
}
if result[len(result)-1]+asteroids[j] == 0 {
j++
result = result[:len(result)-1]
continue
}
result = result[:len(result)-1]
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment