把数组排成最小的数/1038. Recover the Smallest Number
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了把数组排成最小的数/1038. Recover the Smallest Number相关的知识,希望对你有一定的参考价值。
题目描述
Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given {32, 321, 3214, 0229, 87}, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.
Input Specification:
Each input file contains one test case. Each case gives a positive integer N (<=10000) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the smallest number in one line. Do not output leading zeros.
Sample Input:5 32 321 3214 0229 87Sample Output:
22932132143287
1 class Solution 2 { 3 public: 4 string PrintMinNumber(vector<int> num) 5 { 6 string ans = ""; 7 int len = num.size(); 8 if(len==0) 9 return ans; 10 vector<string> nums(len); 11 for(int i = 0; i<len; i++) 12 nums[i] = to_string(num[i]); 13 sort(nums.begin(),nums.end(),cmp); 14 for(int i = 0; i<len; i++) 15 ans+=nums[i]; 16 return ans; 17 } 18 static int cmp(const string &s1,const string &s2) 19 { 20 return s1+s2<s2+s1; 21 } 22 };
以上是关于把数组排成最小的数/1038. Recover the Smallest Number的主要内容,如果未能解决你的问题,请参考以下文章