Skip to content

Commit 30c24d3

Browse files
committed
feat: add rust solution to lc problem: No.0969
No.0969.Pancake Sorting
1 parent 9c91640 commit 30c24d3

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

solution/0900-0999/0969.Pancake Sorting/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,33 @@ func pancakeSort(arr []int) []int {
205205
}
206206
```
207207

208+
### **Rust**
209+
210+
```rust
211+
impl Solution {
212+
pub fn pancake_sort(mut arr: Vec<i32>) -> Vec<i32> {
213+
let mut res = vec![];
214+
for n in (1..arr.len()).rev() {
215+
let mut max_idx = 0;
216+
for idx in 0..=n {
217+
if arr[max_idx] < arr[idx] {
218+
max_idx = idx;
219+
}
220+
}
221+
if max_idx != n {
222+
if max_idx != 0 {
223+
arr[..=max_idx].reverse();
224+
res.push(max_idx as i32 + 1);
225+
}
226+
arr[..=n].reverse();
227+
res.push(n as i32 + 1);
228+
}
229+
}
230+
res
231+
}
232+
}
233+
```
234+
208235
### **...**
209236

210237
```

solution/0900-0999/0969.Pancake Sorting/README_EN.md

+27
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,33 @@ func pancakeSort(arr []int) []int {
194194
}
195195
```
196196

197+
### **Rust**
198+
199+
```rust
200+
impl Solution {
201+
pub fn pancake_sort(mut arr: Vec<i32>) -> Vec<i32> {
202+
let mut res = vec![];
203+
for n in (1..arr.len()).rev() {
204+
let mut max_idx = 0;
205+
for idx in 0..=n {
206+
if arr[max_idx] < arr[idx] {
207+
max_idx = idx;
208+
}
209+
}
210+
if max_idx != n {
211+
if max_idx != 0 {
212+
arr[..=max_idx].reverse();
213+
res.push(max_idx as i32 + 1);
214+
}
215+
arr[..=n].reverse();
216+
res.push(n as i32 + 1);
217+
}
218+
}
219+
res
220+
}
221+
}
222+
```
223+
197224
### **...**
198225

199226
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn pancake_sort(mut arr: Vec<i32>) -> Vec<i32> {
3+
let mut res = vec![];
4+
for n in (1..arr.len()).rev() {
5+
let mut max_idx = 0;
6+
for idx in 0..=n {
7+
if arr[max_idx] < arr[idx] {
8+
max_idx = idx;
9+
}
10+
}
11+
if max_idx != n {
12+
if max_idx != 0 {
13+
arr[..=max_idx].reverse();
14+
res.push(max_idx as i32 + 1);
15+
}
16+
arr[..=n].reverse();
17+
res.push(n as i32 + 1);
18+
}
19+
}
20+
res
21+
}
22+
}

0 commit comments

Comments
 (0)