Asteroid Collision
|Last edited: 2024-5-10
ID
735
Data Type
Stack
Difficulty
Medium
Tags
Simulation
Completed Date
May 9, 2024
Preference
We are given an array asteroids of integers representing asteroids in a row.
For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.
Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.
Example 1:
Example 2:
Example 3:
Constraints:
  • 2 <= asteroids.length <= 104
  • 1000 <= asteroids[i] <= 1000
  • asteroids[i] != 0

题解

 
这个的关键点在于当 resLen==0 的时候,除了一开始啥也没有的条件之外,还包括左边有个大行星把右边的全撞干净了
 
 

乱解