Skip to content

Commit 28e9a70

Browse files
authored
Create 0735-asteroid-collision.cpp
1 parent 0dbde16 commit 28e9a70

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

cpp/0735-asteroid-collision.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
vector<int> asteroidCollision(vector<int>& asteroids) {
4+
vector<int> stk;
5+
6+
for (auto ast: asteroids)
7+
{
8+
while (!stk.empty() && stk.back() > 0 && ast < 0)
9+
{
10+
int diff = ast + stk.back();
11+
if (diff > 0)
12+
{
13+
ast = 0;
14+
}
15+
else if (diff < 0)
16+
{
17+
stk.pop_back();
18+
}
19+
else
20+
{
21+
ast = 0;
22+
stk.pop_back();
23+
}
24+
}
25+
if (ast != 0)
26+
stk.push_back(ast);
27+
}
28+
return stk;
29+
}
30+
};

0 commit comments

Comments
 (0)