【leetcode】找到全部數組中消失的數字

 

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */

int* findDisappearedNumbers(int* nums, int numsSize, int* returnSize){

    int* hash = (int*)calloc(numsSize+1,sizeof(int));

    int i,pst=0;
    for(i=0; i<numsSize; i++)
        hash[nums[i]]++;
    
    for(i=1; i<=numsSize; i++){
        if(!hash[i])
            nums[pst++]=i;
    }

    *returnSize=pst;
    return nums;
}
相關文章
相關標籤/搜索