We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f5e9d79 commit f02b108Copy full SHA for f02b108
asteroidCollision.cpp
@@ -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
13
+ check = true;
14
+ break;
15
16
+ else {
17
18
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
29
30
+ return result;
31
32
+};
0 commit comments