[leetcode]contains duplicate

634 ワード

配列に重複する要素があるかどうかを尋ねます.
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
ハッシュテーブルの単純な適用配列マッピング
bool containsDuplicate(int* nums, int numsSize) {
    if(numsSize == 0 || numsSize == 1)
    return false;
    bool a[1000005];
    memset(a, false, sizeof(a));
    for(int i = 0; i < numsSize; i++)
    {
        if(a[nums[i]] == true)
            return true;
        else
            a[nums[i]] = true;
    }
    return false;
}