i am trying to solve leetcode question :-
https://leetcode.com/problems/largest-number/ Given a list of non negative integers, arrange them such that they form the largest number.
For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. i am trying to sort strings by defining comparator to compare strings by concatinating right-to-left or left-to-right.
The program is giving runtime error. Please help....
int comp(const void* a, const void* b){
int p = *((int *)a);
int q = *((int *)b);
int size = 14;
char * first = (char *)malloc(size * sizeof(char));
char * second = (char *)malloc(size * sizeof(char));
first[0] = "\0";
second[0] = "\0";
sprintf(first, "%d",p);
sprintf(first, "%d",q);
sprintf(second, "%d",q);
sprintf(second, "%d",p);
return -1*strcmp(first, second);
}
char* largestNumber(int* nums, int numsSize) {
if(numsSize <=0)
return NULL;
qsort(nums, numsSize, sizeof(int), comp);
char * result = (char*)malloc(numsSize *5*sizeof(char));
int i;
for(i=0; i<numsSize; i++)
result = strcat(result, nums[i]);
return result;
}