Участник:PinkHedgehog/partition-labels-in-rust
Материал из DISCOPAL
<code-rust> impl Solution {
pub fn partition_labels(s: String) -> Vec<i32> { //Копировать от сих (см. конец кода) struct Letter { l: Vec<char>, first: usize, last: usize, }
let mut stats: Vec<Letter> = Vec::new();
for (i, c) in s.char_indices() { let mut not_found = true; for x in &mut stats{ if x.l[0] == c { x.last = i; not_found = false; break; } } if not_found { stats.push(Letter {l: vec![c], first: i, last: i}) } } stats.sort_by(|x, y| { x.last.cmp(&y.last) }); let mut flag = true; while flag { flag = false; for i in 0..stats.len() { let j = i + 1; if j >= stats.len() { break; } if stats[i].last > stats[j].first { let t = stats.remove(j); for x in t.l { stats[i].l.push(x); } stats[i].first = stats[i].first.min(t.first); stats[i].last = stats[i].last.max(t.last); flag = true; } } } return stats.iter().map(|x| (x.last - x.first + 1) as i32).collect(); //До сих }
} </code-rust>