题设是寻找数组中只出现一次的数,其他数字均出现两次(力扣初级算法的题)
代码如下
#include<stdlib.h>
int* array(int numsSize);
int singleNumber(int* nums, int numsSize);
int main()
{
int numsSize;
scanf("%d",&numsSize);
int* nums;
nums = array(numsSize);
int single = 0;
single = singleNumber(nums,numsSize);
printf("只出现一次的是:%d",single);
free(nums);
return 0;
}
int singleNumber(int* nums, int numsSize){
int ans = 0;
for(int i = 0; i < numsSize; i++){
ans ^= nums[i];**//debug运行到这一步出错Segmentation Fault**
}
return ans;
}
int* array(int numsSize)//返回一个动态数组
{
int* p = (int*)malloc(sizeof(int)*numsSize);
if (p == NULL)
{
printf("未申请到内存");
return 0;
}
for(int i = 0;i <numsSize;i++)
{
scanf("%d",&p[i]);
}
}