Re: [閒聊] 每日leetcode
※ 引述《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
→
01/15 00:00,
10月前
, 1F
01/15 00:00, 1F
→
01/15 09:19,
10月前
, 2F
01/15 09:19, 2F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 1279 之 1549 篇):