File tree 3 files changed +154
-0
lines changed
solution/0700-0799/0735.Asteroid Collision
3 files changed +154
-0
lines changed Original file line number Diff line number Diff line change @@ -134,6 +134,59 @@ public:
134
134
};
135
135
```
136
136
137
+ ### **Rust**
138
+
139
+ ```rust
140
+ impl Solution {
141
+ #[allow(dead_code)]
142
+ pub fn asteroid_collision(asteroids: Vec<i32>) -> Vec<i32> {
143
+ let mut ret_stack = Vec::new();
144
+
145
+ for &a in &asteroids {
146
+ if ret_stack.is_empty() {
147
+ ret_stack.push(a);
148
+ continue;
149
+ }
150
+ if a > 0 {
151
+ ret_stack.push(a);
152
+ continue;
153
+ }
154
+ // Otherwise, peek the top element in the current stack
155
+ if a < 0 {
156
+ if *ret_stack.last().unwrap() < 0 {
157
+ ret_stack.push(a);
158
+ continue;
159
+ }
160
+ let mut explode_flag = false;
161
+ while !ret_stack.is_empty() && *ret_stack.last().unwrap() > 0 {
162
+ let cur_res = *ret_stack.last().unwrap() + a;
163
+ if cur_res < 0 {
164
+ // |a| > |top()|
165
+ assert_ne!(ret_stack.pop(), None);
166
+ } else if cur_res == 0 {
167
+ // |a| == |top()|
168
+ explode_flag = true;
169
+ assert_ne!(ret_stack.pop(), None);
170
+ break;
171
+ } else {
172
+ // |a| < |top()|
173
+ explode_flag = true;
174
+ break;
175
+ }
176
+ }
177
+ if !explode_flag {
178
+ ret_stack.push(a);
179
+ }
180
+ continue;
181
+ }
182
+ assert!(false); // This is impossible
183
+ }
184
+
185
+ ret_stack
186
+ }
187
+ }
188
+ ```
189
+
137
190
### ** Go**
138
191
139
192
``` go
Original file line number Diff line number Diff line change @@ -125,6 +125,59 @@ public:
125
125
};
126
126
```
127
127
128
+ ### **Rust**
129
+
130
+ ```rust
131
+ impl Solution {
132
+ #[allow(dead_code)]
133
+ pub fn asteroid_collision(asteroids: Vec<i32>) -> Vec<i32> {
134
+ let mut ret_stack = Vec::new();
135
+
136
+ for &a in &asteroids {
137
+ if ret_stack.is_empty() {
138
+ ret_stack.push(a);
139
+ continue;
140
+ }
141
+ if a > 0 {
142
+ ret_stack.push(a);
143
+ continue;
144
+ }
145
+ // Otherwise, peek the top element in the current stack
146
+ if a < 0 {
147
+ if *ret_stack.last().unwrap() < 0 {
148
+ ret_stack.push(a);
149
+ continue;
150
+ }
151
+ let mut explode_flag = false;
152
+ while !ret_stack.is_empty() && *ret_stack.last().unwrap() > 0 {
153
+ let cur_res = *ret_stack.last().unwrap() + a;
154
+ if cur_res < 0 {
155
+ // |a| > |top()|
156
+ assert_ne!(ret_stack.pop(), None);
157
+ } else if cur_res == 0 {
158
+ // |a| == |top()|
159
+ explode_flag = true;
160
+ assert_ne!(ret_stack.pop(), None);
161
+ break;
162
+ } else {
163
+ // |a| < |top()|
164
+ explode_flag = true;
165
+ break;
166
+ }
167
+ }
168
+ if !explode_flag {
169
+ ret_stack.push(a);
170
+ }
171
+ continue;
172
+ }
173
+ assert!(false); // This is impossible
174
+ }
175
+
176
+ ret_stack
177
+ }
178
+ }
179
+ ```
180
+
128
181
### ** Go**
129
182
130
183
``` go
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ #[ allow( dead_code) ]
3
+ pub fn asteroid_collision ( asteroids : Vec < i32 > ) -> Vec < i32 > {
4
+ let mut ret_stack = Vec :: new ( ) ;
5
+
6
+ for & a in & asteroids {
7
+ if ret_stack. is_empty ( ) {
8
+ ret_stack. push ( a) ;
9
+ continue ;
10
+ }
11
+ if a > 0 {
12
+ ret_stack. push ( a) ;
13
+ continue ;
14
+ }
15
+ // Otherwise, peek the top element in the current stack
16
+ if a < 0 {
17
+ if * ret_stack. last ( ) . unwrap ( ) < 0 {
18
+ ret_stack. push ( a) ;
19
+ continue ;
20
+ }
21
+ let mut explode_flag = false ;
22
+ while !ret_stack. is_empty ( ) && * ret_stack. last ( ) . unwrap ( ) > 0 {
23
+ let cur_res = * ret_stack. last ( ) . unwrap ( ) + a;
24
+ if cur_res < 0 {
25
+ // |a| > |top()|
26
+ assert_ne ! ( ret_stack. pop( ) , None ) ;
27
+ } else if cur_res == 0 {
28
+ // |a| == |top()|
29
+ explode_flag = true ;
30
+ assert_ne ! ( ret_stack. pop( ) , None ) ;
31
+ break ;
32
+ } else {
33
+ // |a| < |top()|
34
+ explode_flag = true ;
35
+ break ;
36
+ }
37
+ }
38
+ if !explode_flag {
39
+ ret_stack. push ( a) ;
40
+ }
41
+ continue ;
42
+ }
43
+ assert ! ( false ) ; // This is impossible
44
+ }
45
+
46
+ ret_stack
47
+ }
48
+ }
You can’t perform that action at this time.
0 commit comments