2025-07-10 14:13:01 +08:00
|
|
|
struct Solution;
|
2025-09-06 21:58:55 +08:00
|
|
|
|
2025-07-10 14:13:01 +08:00
|
|
|
impl Solution {
|
2025-09-07 01:16:07 +08:00
|
|
|
pub fn pancake_sort(arr: Vec<i32>) -> Vec<i32> {
|
|
|
|
let mut arr = arr;
|
|
|
|
let mut ans = vec![];
|
|
|
|
for i in (1..arr.len()+1).rev() {
|
|
|
|
if arr[i-1] == i as i32 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let mut index = 0;
|
|
|
|
while arr[index] != i as i32 {
|
|
|
|
index += 1;
|
|
|
|
}
|
|
|
|
ans.push((index + 1) as i32);
|
|
|
|
arr[0..index + 1].reverse();
|
|
|
|
ans.push(i as i32);
|
|
|
|
arr[0..i].reverse();
|
|
|
|
println!("{:?}", arr);
|
2025-09-05 11:41:56 +08:00
|
|
|
}
|
2025-09-06 21:58:55 +08:00
|
|
|
ans
|
2025-07-10 14:13:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2025-09-07 01:16:07 +08:00
|
|
|
let sl = Solution::pancake_sort(vec![3, 2, 4, 1]);
|
2025-07-29 11:20:14 +08:00
|
|
|
println!("{:?}", sl);
|
2025-07-10 14:13:01 +08:00
|
|
|
}
|