Re: [閒聊] 每日leetcode

作者: yam276 ('_')   2025-01-14 23:56:11
※ 引述《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
}
}
作者: Niuromem   2025-01-15 00:00:00
別卷了

Links booklink

Contact Us: admin [ a t ] ucptt.com