精華區beta Marginalman 關於我們 聯絡資訊
1431. Kids With the Greatest Number of Candies https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/ 都簡單題就多寫一點 題意: 每個小孩有不同數量的糖果 你有一疊 e 顆糖果 要是每次把 e 顆糖果給一個小孩 能不能比原本擁有最多糖果的小孩多 Code: impl Solution { pub fn kids_with_candies(candies: Vec<i32>, extra_candies: i32) -> Vec<bool> { let max_candies = candies.iter().max().unwrap(); candies .iter() .map(|&x| x + extra_candies >= *max_candies) .collect() } } 一行鏈式解: candies.iter() .map(|&x| x + extra_candies >= *candies.iter().max().unwrap()) .collect() -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.248.143.163 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1748429040.A.244.html