24ms C++ using hash.


  • -7
    X

    class Solution {
    public:
    struct LinkNode{
    int val;
    LinkNode *next;
    LinkNode(int x = 0):val(x),next(NULL){};
    };
    bool containsDuplicate(vector<int>& nums) {
    int len = nums.size();
    LinkNode *records = new LinkNode[len];
    for(int i = 0; i < len; i++){
    int bucket = (nums[i] % len + len) % len;
    LinkNode *temp = &records[bucket] ;
    while(temp->next != NULL){
    temp = temp->next;
    if(temp->val == nums[i]) return true;
    }
    temp->next = new LinkNode(nums[i]);
    }
    return false;
    }
    };


Log in to reply
 

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