Re: [閒聊] 每日leetcode已回收
※ 引述《JIWP (神楽めあ的錢包)》之銘言:
: 846. Hand of Straights
: 有一組整數array hand和一個整數groupsize
: 請問hand裡的元素是否能分成數個groups
: 其中每個group大小為groupsize
: 且group中的每個數字是連續的
: 思路:
一樣使用一個計算frqency的哈希表來實作
Code:
use std::collections::HashMap;
impl Solution {
pub fn is_n_straight_hand(mut hand: Vec<i32>, group_size: i32) -> bool {
hand.sort_unstable();
let mut card_freq = HashMap::new();
for &card in hand.iter() {
*card_freq.entry(card).or_insert(0) += 1;
}
for &card in &hand {
if *card_freq.get(&card).unwrap_or(&0) > 0 {
for i in 0..group_size {
let freq = card_freq.entry(card + i).or_insert(0);
if *freq > 0 {
*freq -= 1;
}
else {
return false;
}
}
}
}
true
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.193.249.242 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1717713759.A.BBA.html
推
06/07 07:50,
1年前
, 1F
06/07 07:50, 1F
推
06/07 08:16,
1年前
, 2F
06/07 08:16, 2F
推
06/07 10:30,
1年前
, 3F
06/07 10:30, 3F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 326 之 1549 篇):