Share a C++ bitset solution


  • 1
    B
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
            bitset<1000> n1;
            bitset<1000> n2;
            for(auto n:nums1){
                n1.set(n);
            }
            for(auto n:nums2){
                n2.set(n);
            }
            vector<int> res;
            n1&=n2;
            for(int i=0;i<1000;i++){
                if(n1[i]==1)
                    res.push_back(i);
            }
            return res;
        }
    

    It should have been much faster by using bit manipulation, but it does not.
    It cannot be more appreciated if anybody could give a analysis.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.