forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path89.c
More file actions
34 lines (27 loc) · 663 Bytes
/
89.c
File metadata and controls
34 lines (27 loc) · 663 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
#include <stdio.h>
#include <stdlib.h>
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* grayCode(int n, int* returnSize) {
*returnSize = 1 << n;
int *gray = (int *)malloc(*returnSize * sizeof(int));
int i;
for (i = 0; i < *returnSize; i++) {
gray[i] = (i >> 1) ^ i;
}
return gray;
}
int main() {
int n = 2;
int returnSize = 0;
int* ans = grayCode(n, &returnSize);
int i;
for (i = 0; i < returnSize; i++) {
printf("%d ", ans[i]);
}
printf("\n");
free(ans);
return 0;
}