LeetCode 2032. 至少在两个数组中出现的值(哈希/位运算)
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 2032. 至少在两个数组中出现的值(哈希/位运算)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 題目
- 2. 解題
- 2.1 哈希查找
- 2.2 位運算
1. 題目
給你三個整數數組 nums1、nums2 和 nums3 ,請你構造并返回一個 不同 數組,且由 至少 在 兩個 數組中出現的所有值組成。
數組中的元素可以按 任意 順序排列。
來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/two-out-of-three
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
2. 解題
2.1 哈希查找
class Solution { public:vector<int> twoOutOfThree(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3) {unordered_set<int> s1(nums1.begin(), nums1.end()), s2(nums2.begin(), nums2.end()), s3(nums3.begin(), nums3.end());unordered_set<int> ans;for(auto n : nums1){if(s2.find(n)!=s2.end() || s3.find(n)!=s3.end())ans.insert(n);}for(auto n : nums2){if(s1.find(n)!=s1.end() || s3.find(n)!=s3.end())ans.insert(n);}return vector<int> (ans.begin(), ans.end());} };20 ms 26.5 MB C++
2.2 位運算
- 用3個二進制位表示每個數在三個數組里的狀態是否存在
- 檢查狀態的二進制值是否有2個以上的1
8 ms 24.5 MB C++
我的CSDN博客地址 https://michael.blog.csdn.net/
長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!
總結
以上是生活随笔為你收集整理的LeetCode 2032. 至少在两个数组中出现的值(哈希/位运算)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 2210. 统计数组中
- 下一篇: Paddle 使用预训练模型 实现快递单