опять же поделюс своим решением тоже!
хотя оно не такое оптимальное судя по всему)
use std::iter::zip;
impl Solution {
pub fn can_change(start: String, target: String) -> bool {
{
let s1 = start.chars().filter(|it| *it != '_').collect::<String>();
let s2 = target.chars().filter(|it| *it != '_').collect::<String>();
if s1 != s2 {
return false;
}
};
let lindices1 = Self::collect(&start, 'L');
let rincides1 = Self::collect(&start, 'R');
let lindices2 = Self::collect(&target, 'L');
let rindices2 = Self::collect(&target, 'R');
for (l1, l2) in zip(lindices1, lindices2){
if l1 <= l2{
return false
}
}
for (r1, r2) in zip(rincides1, rindices2){
if r1 >= r2{
return false
}
}
true
}
fn collect(s:&str, target:char)->Vec<usize>{
s.char_indices().filter(|(_, ch)|*ch == 'L').map(|it|it.0).collect()
}
}
перейти
разрешите поделиться своим решением начальник!
use std::collections::HashSet;
use std::ops::Sub;
impl Solution {
pub fn find_champion(n: i32, edges: Vec<Vec<i32>>) -> i32 {
let players = (0..n).collect::<HashSet<_>>();
let loosers: HashSet<_> = edges.iter().map(|it| it[1]).collect();
let new = players.sub(&loosers);
if new.len() != 1 {
return -1;
}
new.into_iter().next().unwrap()
}
}
перейти