Given a list of non negative integers, arrange them such that they form the largest number.
For example, given [3, 5, 9], and the largest formed number is 9534330.
Note:the result May is very large, so you need to return a string instead of an integer.
Ideas:
To arrange the numbers, the first big is in front.
Compare the first of each number, which is difficult to implement without knowing the number of bits, so convert int to string and then compare.
Because the connection operation of string is very convenient, the size of the first is converted to the size of the string after the connection is compared.
classSolution { Public: Static BOOLcmpConst string&S1,Const string&S2) {//Note Static,const, the use of references return(S1+S2) > (S2+S1);//> indicates descending order } stringLargestnumber (vector<int>&nums) { intSize =nums.size (); StringStream SS; Vector<string>s_nums (size); stringRET =""; for(inti =0; i < size; i++) {SS<<Nums[i]; SS>>S_nums[i]; Ss.clear (); } sort (S_nums.begin (), S_nums.end (), CMP); if(s_nums[0]=="0")returns_nums[0]; for(inti =0; i < size; i++) {ret+=S_nums[i]; } returnret; }};
179. Largest number (INT, String)