剑指OFFER 数组中重复的数字

剑指OFFER 数组中重复的数字

使用哈希表来完成

class Solution {
public:
    // Parameters:
    //        numbers:     an array of integers
    //        length:      the length of array numbers
    //        duplication: (Output) the duplicated number in the array number
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    
    map<int,int> m;
    bool duplicate(int num[], int length, int* duplication) {
        for(int i=0;i<length;i++)
        {
            m[num[i]]++;
            if(m[num[i]] > 1)
            {
                (*duplication) = num[i];
                return true;
            }
        }
        return false;
    }
};
原文地址:https://www.cnblogs.com/virgildevil/p/12188398.html