2022年11月22日 星期二

[442] Find All Duplicates in an Array

第一版!!!
竟然忘了count++ 真是太蠢了= = 
更新:後面看一看除了先sorting 以外好像也沒什麼特別的(thinking圖)
那就先這樣好了XD~~~
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* findDuplicates(int* nums, int numsSize, int* returnSize){
int* hash=calloc(100001,sizeof(int));
int count=0;
for (int i=0;i<numsSize; i++)
{
hash[nums[i]]++;
if (hash[nums[i]]==2)
count++;
}
int *ret= calloc(count, sizeof(int));
*returnSize = count;
count=0;
for (int i=1;i<100001;i++)
if (hash[i]>=2)
ret[count++]=i;
return ret;
}



沒有留言:

張貼留言