Skip to content

Commit f02b108

Browse files
authored
735. Asteroid Collision
1 parent f5e9d79 commit f02b108

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

asteroidCollision.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
vector<int> asteroidCollision(vector<int>& asteroids) {
4+
stack <int> st;
5+
for (int i=0; i<asteroids.size(); i++){
6+
bool check = false;
7+
while (!st.empty() && asteroids[i]<0 && st.top()>0){
8+
if (abs(st.top()) < abs(asteroids[i])) {
9+
st.pop();
10+
}
11+
else if (abs(st.top()) == abs(asteroids[i])) {
12+
st.pop();
13+
check = true;
14+
break;
15+
}
16+
else {
17+
check = true;
18+
break;
19+
}
20+
}
21+
if (!check) {
22+
st.push(asteroids[i]);
23+
}
24+
}
25+
vector<int> result(st.size());
26+
for (int i = st.size() - 1; i >= 0; i--) {
27+
result[i] = st.top();
28+
st.pop();
29+
}
30+
return result;
31+
}
32+
};

0 commit comments

Comments
 (0)