24ms, c++ solution


  • -5
    Z

    class Solution {
    public:
    bool containsDuplicate(vector<int>& nums) {

        bool array[0xFFFFF] = {};
        bool withDuplicates = false;
        
        for(int i = 0; i< nums.size(); i++)
        {
            if(array[nums[i]]) {
                withDuplicates = true;
                break;
            }
            else{
                array[nums[i]] = true;
            }
        }
    
        return withDuplicates;
    }
    

    };


  • 0
    N

    Could you explain your solution?
    I'm not clear about the use of " bool array[0xFFFFF] = {};" and " if(array[nums[i]])"........


  • 0
    S
    This post is deleted!

  • 0
    S

    I guess array[0xFFFFF] is supposed to be an array having the size of all possible integers. Although I think it might be array[0xFFFFFFFF] if sizeof(int) is 4. Also it has to assume every int in nums is positive.


Log in to reply
 

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