作者yam276 (三角初華的事件視界)
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Tue Jan 14 23:56:11 2025
※ 引述《dont (dont)》之銘言:
: 2657. Find the Prefix Common Array of Two Arrays
: ## 思路
: 掃Array, 把A[i], B[i]的bit設1
: 檢查兩個mask &之後的bits數
好久沒寫 腦袋當機
邊看邊復健 解說跟我說用HashSet
Code:
use std::collections::HashSet;
impl Solution {
pub fn find_the_prefix_common_array(a: Vec<i32>, b: Vec<i32>) -> Vec<i32>
{
let mut seen = HashSet::new();
let mut common_count = 0;
let mut result = Vec::with_capacity(a.len());
for (&x, &y) in a.iter().zip(b.iter()) {
if seen.contains(&x) {
common_count += 1;
} else {
seen.insert(x);
}
if seen.contains(&y) {
common_count += 1;
} else {
seen.insert(y);
}
result.push(common_count);
}
result
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.193.249.242 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1736870173.A.02D.html
→ Niuromem: 別卷了 01/15 00:00
→ DJYOMIYAHINA: 別卷了 01/15 09:19