-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0268-missing-number.c
42 lines (42 loc) · 1.06 KB
/
0268-missing-number.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//计算n+1个数的和减去数组的和
int missingNumber(int* nums, int numsSize) {
int sum = (numsSize + 1) * numsSize / 2, numsum = 0;
for(int i = 0; i < numsSize; i++) {
numsum += nums[i];
}
return sum - numsum;
}
//建立新数组查找
int missingNumber(int* nums, int numsSize) {
int* res = (int *)calloc(numsSize + 1, sizeof(int));
for(int i = 0; i < numsSize; i++) {
res[nums[i]] = 1;
}
for(int j = 0; j < numsSize + 1; j++) {
if(res[j] == 0) {
return j;
}
}
return -1;
}
//先排序后查找
int missingNumber(int* nums, int numsSize) {
int i, j, min, min_index;
for(i = 0; i < numsSize; i++) {
min = nums[i];
min_index = i;
for(j = i; j < numsSize; j++) {
if(nums[j] < min) {
min = nums[j];
min_index = j;
}
}
j = nums[i];
nums[i] = min;
nums[min_index] = j;
}
for(i = 0; i < numsSize; i++)
if(nums[i] != i)
return i;
return numsSize;
}