原题:
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements of [1, n] inclusive that do not appear in this array.
Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
Example:
Input: [4,3,2,7,8,2,3,1] Output: [5,6]思路:
循环一个做标记
第二次查找哪个数没有被标记就好,(我第一次竟然只是想着做排序然后再遍历。。。 我真是用qsort着魔了。。。)
代码如下:
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* findDisappearedNumbers(int* nums, int numsSize, int* returnSize) {
int* flag;
flag=(int*)malloc(sizeof(int)*(numsSize+1));
memset(flag,0,sizeof(int));
for(int n=0;n<numsSize;n++)
{
*(flag+*(nums+n))=1;
}
int* result;
result=(int*)malloc(sizeof(int)*numsSize);
for(int n=1;n<=numsSize;n++)
{
if(*(flag+n)!=1)
{
*(result+*returnSize)=n;
*returnSize+=1;
}
}
return result;
}
慎用排序函数。。。
慎用排序。
慎用