0
点赞
收藏
分享

微信扫一扫

leetcode 周赛 287

沪钢木子 2022-04-22 阅读 27
rust

2224 转化时间需要的最少操作数

impl Solution {
pub fn convert_time(current: String, correct: String) -> i32 {
    let (h0, m0) = current.split_once(":").unwrap();
    let (h0, m0) = (h0.parse::<i32>().unwrap(), m0.parse::<i32>().unwrap());
    let (h1, m1) = correct.split_once(":").unwrap();
    let (h1, m1) = (h1.parse::<i32>().unwrap(), m1.parse::<i32>().unwrap());

    let mut need = (h1-h0)*60 + (m1-m0);
    let mut ans = 0;
    for unit in [60, 15, 5, 1] {
        ans += need / unit;
        need %= unit;
    }
    ans 
}
}

2225 找出输掉零场或一场比赛的玩家

impl Solution {
pub fn find_winners(matches: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
    use std::collections::BTreeMap;

    let mut win_cnt = BTreeMap::new();
    let mut lose_cnt = BTreeMap::new();
    for mat in matches {
        let (win, lose) = (mat[0], mat[1]);
        win_cnt.entry(win).and_modify(|e| *e += 1).or_insert(1);
        lose_cnt.entry(lose).and_modify(|e| *e += 1).or_insert(1);
    }
    let mut ans = vec![vec![], vec![]];
    for (win, _) in win_cnt {
        if lose_cnt.get(&win).is_none() {
            ans[0].push(win);
        }
    }
    for (lose, cnt) in lose_cnt {
        if cnt == 1 {
            ans[1].push(lose);
        }
    }

    ans
}

}

2226 每个小孩最多能分到多少糖果

impl Solution {
pub fn maximum_candies(candies: Vec<i32>, k: i64) -> i32 {
    let mut left = 1;
    let mut right = *candies.iter().max().unwrap();
    let mut last_success = 0;
    while left <=right {
        let candidate = (left + right)/2;

        let mut cnt = 0;
        for candy in &candies {
            cnt += (candy/candidate) as i64;
        }
        if cnt >= k {
            last_success = candidate;
            left = candidate + 1;
        } else {
            right = candidate - 1;
        }
    }
    last_success
}

}

2227 加密解密字符串

struct Encrypter {
    keys: Vec<char>,
    values: Vec<String>,
    dictionary: Vec<String>,
}


/**
 * `&self` means the method takes an immutable reference.
 * If you need a mutable reference, change it to `&mut self` instead.
 */
impl Encrypter {

    fn new(keys: Vec<char>, values: Vec<String>, dictionary: Vec<String>) -> Self {
        Encrypter { keys, values, dictionary }
    }

    fn encrypt(&self, word1: String) -> String {
        let mut result = String::new();
        for c in word1.chars() {
            // println!("{} {}", c, c as i32);
            let index = self.keys.iter().position(|&x| x == c);
            if index.is_none() {
                return "".to_string();
            }
            result.push_str(&self.values[index.unwrap()]);
        }
        result
    }

    fn decrypt(&self, word2: String) -> i32 {
        let mut res = 0;
        for dic in &self.dictionary {
            if self.encrypt(dic.clone()) == word2 {
                res += 1;
            }
        }
        res
    }
}

举报

相关推荐

【LeetCode】第287场周赛题解

第287场周赛

Leetcode - 周赛385

Leetcode - 周赛424

LeetCode --- 414周赛

leetcode 周赛 286

leetcode 周赛 364

0 条评论