< 返回版块

kingeasternsun 发表于 2021-11-20 15:58

Tags:rust,iterator,leetcode


Ext Link: https://zhuanlan.zhihu.com/p/434712389

评论区

写评论
作者 kingeasternsun 2021-12-05 22:09

383 题目


    impl Solution {
        pub fn can_construct(ransom_note: String, magazine: String) -> bool {
            let mut m = vec![0u32; 26];
            magazine
                .as_bytes()
                .iter()
                .for_each(|x| m[(x - b'a') as usize] = m[(x - b'a') as usize] + 1);

            ransom_note.as_bytes().iter().all(|x| {
                if m[(x - b'a') as usize] == 0 {
                    return false;
                }
                m[(x - b'a') as usize] = m[(x - b'a') as usize] - 1;
                true
            })
        }
    }
作者 kingeasternsun 2021-11-25 11:01

漂亮

👇
dlhxzb:

pub fn detect_capital_use(word: String) -> bool {
    let mut word = word.chars();
    let first_is_upper = if let Some(first) = word.next(){
        first.is_uppercase()
    } else {
        return true;
    };
    
    let mut second_is_upper = None;
    !word.any(|c|{
        if second_is_upper.is_none() {
            second_is_upper = Some(c.is_uppercase())
        }
        (!first_is_upper && c.is_uppercase())
        || second_is_upper != Some(c.is_uppercase())
    })
}
dlhxzb 2021-11-23 17:09
pub fn detect_capital_use(word: String) -> bool {
    let mut word = word.chars();
    let first_is_upper = if let Some(first) = word.next(){
        first.is_uppercase()
    } else {
        return true;
    };
    
    let mut second_is_upper = None;
    !word.any(|c|{
        if second_is_upper.is_none() {
            second_is_upper = Some(c.is_uppercase())
        }
        (!first_is_upper && c.is_uppercase())
        || second_is_upper != Some(c.is_uppercase())
    })
}
1 共 3 条评论, 1 页