Press "Enter" to skip to content

花花酱 LeetCode 735. Asteroid Collision

Problem:

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:

Example 4:

Note:

  • The length of asteroids will be at most 10000.
  • Each asteroid will be a non-zero integer in the range [-1000, 1000]..
Idea:
Simulation

Time complexity: O(n), Space complexity: O(n)

Solution:

C ++

 

请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.

Buy anything from Amazon to support our website
您可以通过在亚马逊上购物(任意商品)来支持我们

Paypal
Venmo
huahualeetcode
微信打赏

Be First to Comment

Leave a Reply