forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path268.c
More file actions
37 lines (28 loc) · 638 Bytes
/
268.c
File metadata and controls
37 lines (28 loc) · 638 Bytes
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
#include <stdio.h>
#include <assert.h>
int missingNumber0(int* nums, int numsSize) {
long long sum = 0;
int i;
for (i = 0; i < numsSize; i++) {
sum += nums[i];
}
long long prod = (numsSize + 1) * numsSize / 2;
return prod - sum;
}
int missingNumber(int* nums, int numsSize) {
int ans = 0;
int i;
for (i = 0; i < numsSize; i++) {
ans ^= nums[i];
ans ^= i + 1;
}
return ans;
}
int main() {
int a[] = {0, 1, 3};
assert(missingNumber(a, 3) == 2);
int b[] = {1, 0};
assert(missingNumber(b, 2) == 2);
printf("all tests passed!\n");
return 0;
}