C solution


  • 0
    J

    void swap(int *a, int *b)
    {
    int temp = 0;

    temp = *a;
    *a = *b;
    *b = temp;
    

    }

    int isLess(int a, int b)
    {
    int ret = 0;
    int i = 0;
    int k = 0;
    char arryA[10] = { 0 };
    int sizeA = 0;
    char arryB[10] = { 0 };
    int sizeB = 0;
    char arryAMerge[20] = { 0 };
    char arryBMerge[20] = { 0 };

    do
    {
    	arryA[i++] = a % 10;
    	a /= 10;
    	sizeA++;
    } while (a > 0);
    
    i = 0;
    do
    {
    	arryB[i++] = b % 10;
    	b /= 10;
    	sizeB++;
    } while (b > 0);
    
    
    for (i = 0; i < sizeA; i++)
    {
    	arryAMerge[k] = arryA[sizeA - i - 1];
    	arryBMerge[k + sizeB] = arryA[sizeA - i - 1];
    	k++;
    }
    
    for (i = 0; i < sizeB; i++)
    {
    	arryAMerge[k] = arryB[sizeB - i - 1];
    	arryBMerge[k - sizeA] = arryB[sizeB - i - 1];
    	k++;
    }
    
    for (i = 0; i < sizeA + sizeB; i++)
    {
    	if (arryAMerge[i] < arryBMerge[i])
    	{
    		ret = 1;
    		break;
    	}
    	else if (arryAMerge[i] > arryBMerge[i])
    	{
    		break;
    	}
    }
    
    return ret;
    

    }

    void QuickSort(int * arr, int size)
    {
    int i = 0;
    int idxWall = 0;
    int pivot = 0;

    if (size <= 1)
    {
    	return;	
    }
    
    pivot = arr[size -1];
    for (i = 0; i < size - 1; i++)
    {
    	if (1 == isLess(arr[i] , pivot))
    	{
    		if (i > idxWall)
    		{
    			swap(&arr[idxWall], &arr[i]);
    		}
    	    idxWall++;
    	}
    }
    swap(&arr[idxWall], &arr[size - 1]);
    
    QuickSort(&arr[0], idxWall);
    QuickSort(&arr[idxWall + 1], size - idxWall - 1);
    

    }

    int buildString(char *result, int nums[], int numsSize)
    {
    int i = 0;
    int j = 0;
    int k = 0;
    int leadZero = 1;
    int a;
    char arry[10] = {0};
    int size = 0;

    for (i = numsSize - 1; i >= 0; i--)
    {
       a = nums[i];
       j = 0;
       size = 0;
       do
       {
          arry[j++] = a % 10;
          a /= 10;
          size++;
       }while(a > 0);
       
       while(--size >= 0)
       {
           result[k++] = arry[size] + '0';
       }
    }
    return k;
    

    }

    char* largestNumber(int* nums, int numsSize) {
    int size;
    char *result;
    result = (char *)malloc(numsSize * 10 + 1);
    memset(result, 0, numsSize * 10 + 1);

    QuickSort(nums, numsSize);
    size =buildString(result, nums, numsSize);
    if('0' == result[0])
    {
        size = 1;
    }
    result[size] = '\0';
    return result;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.