本帖最后由 happy886rr 于 2016-11-14 00:49 编辑
故题重游,C语言版:
Submission Details
221 / 221 test cases passed.
Status: Accepted
Runtime: 0 ms | char outStr[]; | | int CompNums(const void* a, const void* b){ | | char* str1=*(char**)a; | | char* str2=*(char**)b; | | int i, L1=strlen(str1), L2=strlen(str2); | | for(i=0; i<10; i++){ | | if(*str1=='\0'){str1-=L1;} | | if(*str2=='\0'){str2-=L2;} | | if(*str1 != *str2){break;} | | str1++, str2++; | | } | | return *str2-*str1; | | } | | char* largestNumber(int* nums, int numsSize) { | | int i; | | outStr[0]=0; | | char* p[numsSize]; | | for(i=0; i<numsSize; i++){p[i]=(char*)malloc(sizeof(char)*16);sprintf(p[i], "%d\0", nums[i]);} | | qsort(p, numsSize, sizeof(char*), CompNums); | | for(i=0; i<numsSize; i++){strcat(outStr, p[i]);} | | return outStr[0]=='0'?"0":outStr; | | }COPY |
>>>
补充,针对本帖所有回帖的C代码,在100万次的测试当中,只有21楼aa77dd@163.com兄的代码速度最快,100万次耗时0.7秒,本人用itoa函数也只能做到100万次耗时1.1秒。其他C语言方案均耗时数秒,并且存在严重漏洞。
>>>
另外小于5000次的测试根本无法体现性能,几乎都是0毫秒。只在百万次的测试上个别方案才会出现极大的漏洞。 |