Skip to content

Commit 0437209

Browse files
authored
feat: add rust solution to lc problem: No.0735 (doocs#1255)
1 parent f95e192 commit 0437209

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

solution/0700-0799/0735.Asteroid Collision/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,59 @@ public:
134134
};
135135
```
136136
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+
137190
### **Go**
138191

139192
```go

solution/0700-0799/0735.Asteroid Collision/README_EN.md

+53
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,59 @@ public:
125125
};
126126
```
127127
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+
128181
### **Go**
129182

130183
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

0 commit comments

Comments
 (0)