-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode2.c
More file actions
30 lines (28 loc) · 789 Bytes
/
Leetcode2.c
File metadata and controls
30 lines (28 loc) · 789 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
int compare(const void* a, const void* b) {
const char** ia = (const char*)a;
const char** ib = (const char*)b;
char* string1[20] = { '\0' };
char* string2[20] = { '\0' };
strcat(string1, *ia);
strcat(string1, *ib);
strcat(string2, *ib);
strcat(string2, *ia);
return -1*strcmp(string1, string2);
}
char * largestNumber(int* nums, int numsSize){
char* zero = "0";
char** temp = malloc(sizeof(char*) * numsSize);
for (int i = 0; i < numsSize; i++) {
temp[i] = malloc(sizeof(char) * 10);
sprintf(temp[i], "%d", nums[i]);
}
qsort(temp, numsSize, sizeof(char*) , compare);
if (!strcmp(temp[0], "0")) { return "0"; }
char* ans = malloc(sizeof(char) * 500);
ans[0] = '\0';
for (int i = 0; i < numsSize; i++) {
strcat(ans, temp[i]);
}
printf("%s", ans);
return ans;
}