Skip to content

Create 0179-largest-number.c #2308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions c/0179-largest-number.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Compare two numbers in a concatenated form.
// eg: 12 and 34 will be compared as 1234 vs 3412
int compareInt(const void *a, const void *b) {
int i = 10;
int j = 10;
int x = *(int*)a;
int y = *(int*)b;

while (x /= 10) i *= 10;
while (y /= 10) j *= 10;

return (((unsigned int)*(int*)b * i) + *(int*)a) > (((unsigned int)*(int*)a * j) + *(int*)b);
}

char * largestNumber(int* nums, int numsSize){
char *res = NULL;
int i, len, pos;

if (numsSize < 0) {
return res;
}

// Sort the array with specified comparaotor.
qsort(nums, numsSize, sizeof(int), compareInt);

// Caculate the length of the return string.
len = 1;
for (i = 0; i < numsSize; i++) len += snprintf(NULL, 0, "%d", nums[i]);
res = calloc(len, sizeof(char));

// If the firs element of sorted array is 0,
// return a single digit of 0 no matter how long is the string.
if (nums[0] == 0) {
res[0] = '0';
return res;
}

// Print all nums to the return string.
pos = 0;
for (i = 0; i < numsSize; i++) {
pos += snprintf(res + pos, len, "%d", nums[i]);
}

return res;
}