-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path735.py
33 lines (30 loc) · 1016 Bytes
/
735.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#==================================================
#==> Title: asteroid-collision
#==> Author: Zhang zhen
#==> Email: hustmatnoble.gmail.com
#==> GitHub: https://github.com/MatNoble
#==> Date: 1/15/2021
#==================================================
"""
https://leetcode-cn.com/problems/asteroid-collision/
"""
class Solution:
def asteroidCollision(self, asteroids):
n = len(asteroids)
stack = []
for i in range(n):
ast = asteroids[i]
if ast > 0:
stack.append(ast)
else:
while stack and stack[-1] > 0 and stack[-1] < - ast:
stack.pop()
if len(stack) != 0 and stack[-1] == -ast:
stack.pop()
elif len(stack) == 0 or stack[-1] < 0:
stack.append(ast)
return stack
mat = Solution()
a = [5,10,-5]
a = [-2,-1,1,2]
mat.asteroidCollision(a)