use std::mem;
impl Solution {
pub fn permutation(s: String) -> Vec<i32> {
let mut start = 0;
let mut end = 0;
let mut search = false;
let mut res = vec!();
(0..=s.len()).for_each(|num| res.push( (num+1) as i32));
for (i, item) in s.chars().enumerate() {
if item == 'I' {
if search {
end = i;
search = false;
Self::reverse(&mut res, start, end);
}
}
if item == 'D' {
if search { continue;}
start = i;
search = true;
}
}
if search {
Self::reverse(&mut res, start, s.len());
}
res
}
fn reverse(res: &mut Vec<i32>, mut start: usize, mut end: usize) {
while end > start {
let temp = res[start];
res[start] = res[end];
res[end] = temp;
start += 1;
end -= 1;
}
}
}
in cpp, we can do void reverse( BidirIt first, BidirIt last );
, I tried mem::swap(&mut res[start], &mut res[end]);
in my reverse function, it failed on multiple-borrows error. Feel like the code have space to be more idiomatic and succinct, Any suggestions is appreciate it!