Largest Number RunTime Error at case [1, 1, 1, 1]


  • 0
    Z

    const int g_maxNumLen = 10;

    int compare(const void* strNum1, const void* strNum2)

    {

    char *g_str1 = new char[g_maxNumLen * 2 + 1];
    char *g_str2 = new char[g_maxNumLen * 2 + 1];
    strcpy(g_str1, *(const char**)strNum1);
    strcat(g_str1, *(const char**)strNum2);
    
    strcpy(g_str2, *(const char**)strNum2);
    strcat(g_str2, *(const char**)strNum1);
    
    int res = strcmp(g_str1, g_str2);
    delete[] g_str1;
    delete[] g_str2;
    if (res == 1)return -1;
    if (res == -1)return 1;
    return res;
    

    }

    class Solution {

    public:

    string largestNumber(vector<int>& nums) {
    	int len = nums.size();
    	string str;
    	if (len == 0)return str;
    
    	char** strNumbers = (char**)(new int[len]);
    	for (int i = 0; i < len; i++)
    	{
    		strNumbers[i] = new char[g_maxNumLen + 1];
    		sprintf(strNumbers[i], "%d", nums[i]);
    	}
    
    	qsort(strNumbers, len, sizeof(char*), compare);
    	for (int i = 0; i < len; i++)
    		str.append(strNumbers[i]);
    	for (int i = 0; i < len; i++)
    		delete[] strNumbers[i];
    	delete[] strNumbers;
    
    	return str;
    }
    

    };


Log in to reply
 

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