首页 > 代码库 > 剑指Offer31 把数组排成最小的数

剑指Offer31 把数组排成最小的数

 1 /************************************************************************* 2     > File Name: 31_SortArrayMin.cpp 3     > Author: Juntaran 4     > Mail: JuntaranMail@gmail.com 5     > Created Time: 2016年09月02日 星期五 11时10分42秒 6  ************************************************************************/ 7  8 #include <stdio.h> 9 #include <string>10 #include <bits/stdc++.h>11 12 using namespace std;13 14 string itos(int x)15 {16     return (x>9 ? itos(x/10) : "") + char(x%10 + 0);17 }18 19 bool cmp(const void *a,const void *b)20 {21     return (itos(a) + itos(b)) < (itos(b) + itos(a));22 }23 24 bool compare(int a, int b)25 {26     return (itos(a) + itos(b)) < (itos(b) + itos(a));27 }28 29 string PrintMinNumber(int* nums, int length)30 {31     string s = "";32     if (nums==NULL || length<=0)33         return s;34 //    sort(nums, nums+length, cmp);35     qsort(nums, length, sizeof(int), cmp);36     37     for (int i = 0; i < length; ++i)38         s += itos(nums[i]);39     cout << s << endl;40     return s;41 }42 43 int main()44 {45     int nums[] = {3, 32, 321};46     int length = 3;47     PrintMinNumber(nums, length);48     49     return 0;50 }

 

剑指Offer31 把数组排成最小的数