22ms C++ Solution, beats 100%


  • 0
    W
    bool containsDuplicate(vector<int>& nums) {
        int m = 16, n = nums.size();
        while(m < n){
            m <<= 1;
        }
        m <<= 1;
        int index[m]{0};
        int *is = index, *ip = is, *ie = is + m;
        int zcount = 0;
        m--;
        int *ps = nums.data(), *pe = ps + n, *p=ps;
        for(; p < pe; ++p){
            int *ip = is + (*p & m);
            for(; *ip != 0; ){
                if(*ip == *p){
                    return true;
                }
                if(++ip == ie){
                    ip = is;
                }
            }
            if(*p == 0 && zcount ++ >= 1){
                return true;
            }
            *ip = *p;
        }
        return false;
    }

Log in to reply
 

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