We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
题目描述:
给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数。
示例 1:
输入: [10,2] 输出: 210
示例 2:
输入: [3,30,34,5,9] 输出: 9534330
说明: 输出结果可能非常大,所以你需要返回一个字符串而不是整数。
解题思路: 这题比较简单,但是比较大小的方法是看的网上的,若ab>ba,则a>b,如9和34,934>349,所以即可让9排在34前面。主要是想一直使用C编程,所以自己写一个比较大小的函数,最后排完之后再连接输出就好。注意:之前一直使用的malloc分配内存,总报类似数组溢出的错误,之后就改成用calloc来分配(我暂时也不懂为毛)。。。
C解题:
int compa(int,int); int compa(int num1,int num2){ if(num1 == 0) return -1; if(num2 == 0) return 1; int pow1 = 0,pow2 = 0; int tmp1 = num1,tmp2 = num2; while (tmp1) { pow1++; tmp1 = tmp1/10; } while (tmp2) { pow2++; tmp2 = tmp2/10; } if ( (num1*pow(10,pow2)+num2) > (num2*pow(10,pow1)+num1) ) return 1; else return -1; } char * largestNumber(int* nums, int numsSize){ char *result = (char*)calloc(numsSize*10,sizeof(char)); for (int i = 0; i < numsSize; i++) { for (int j = i+1; j < numsSize; j++) { if (compa(nums[i],nums[j])<0) { int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; } } char *s1=(char*)calloc(11,sizeof(char)); sprintf(s1,"%d",nums[i]); strcat(result,s1); } int aaa = atoi(result); if(aaa == 0) return '0'; return result; }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目描述:
给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数。
示例 1:
示例 2:
说明: 输出结果可能非常大,所以你需要返回一个字符串而不是整数。
解题思路:
这题比较简单,但是比较大小的方法是看的网上的,若ab>ba,则a>b,如9和34,934>349,所以即可让9排在34前面。主要是想一直使用C编程,所以自己写一个比较大小的函数,最后排完之后再连接输出就好。注意:之前一直使用的malloc分配内存,总报类似数组溢出的错误,之后就改成用calloc来分配(我暂时也不懂为毛)。。。
C解题:
The text was updated successfully, but these errors were encountered: