Re: [闲聊] 每日leetcode

楼主: dont   2025-01-14 19:16:09
2657. Find the Prefix Common Array of Two Arrays
## 思路
扫Array, 把A[i], B[i]的bit设1
检查两个mask &之后的bits数
## Code
```cpp
class Solution {
public:
vector<int> findThePrefixCommonArray(vector<int>& A, vector<int>& B) {
int n = A.size();
bitset<51> maskA=0, maskB=0;
vector<int> res(n, 0);
for (int i=0; i<n; ++i) {
maskA[A[i]] = 1;
maskB[B[i]] = 1;
res[i] = (maskA & maskB).count();
}
return res;
}
};
```

Links booklink

Contact Us: admin [ a t ] ucptt.com