To rank the array into the smallest number

Source: Internet
Author: User

The title description enters an array of positive integers, stitching up all the numbers in the array into a number, and printing the smallest of all the numbers that can be stitched together. For example, enter the array {3,32,321}, then print out the minimum number that these three numbers can be ranked as 321323.
/*对vector容器内的数据进行排序,按照 将a和b转为string后   若 a+b<b+a  a排在在前 的规则排序,   如 2 21 因为 212 < 221 所以 排序后为 21 2     to_string() 可以将int 转化为string */
Class Solution {public:    static BOOL cmp (int a,int b) {        string a= "";        String b= "";        A+=to_string (a);        A+=to_string (b);        B+=to_string (b);        B+=to_string (a);        Return a<b;    }        String Printminnumber (vector<int> numbers) {        string answer= "";        Sort (Numbers.begin (), Numbers.end (), CMP);        for (int i=0;i<numbers.size (); i++) {            answer+=to_string (numbers[i]);        }        return answer;}                    ;

  

Class Solution {public:    string printminnumber (vector<int> numbers) {        string result;       int length=numbers.size ();        if (length<=0)             return Result;       stringstream stream;        //string *str=new string[length];       vector<string > vec;       for (int i=0;i<length;i++)          {            //Method 1: Use To_ String converts a number to a string             vec.push_back (to_string (Numbers[i]));             //Method 2: Convert numbers to strings with the help of a string stream StringStream nbsp;            /*string str;             stream<<numbers[i];             stream>>str;             vec.push_back (str);             stream.clear ();  //Remember to clear              */        }        sort (Vec.begin (), Vec.end (), comp);        for (int i=0; i<length;i++)          {              result+=vec[i];          }        return result;    }     static BOOL Comp (string str1,string str2)  // If the comp function is placed outside, the static keyword can be removed for the following reasons:     {                            The comparison function in   //sort compare to be declared as a static member function or a global function, not as a normal member function, or an error will be made.                                     // Because a non-static member function is dependent on a specific object, and the Std::sort function is global, it is no longer possible to sort the         string s1= str1+str2;      //with non-static member functions. A static member function or global function is independent of the object and can be accessed independently without creating any         string s2=str2+str1;  The      //object instance can be accessed. Static member functions can not invoke non-static members of the class at the same time.         return S1&Lt;s2;     }}; 

  

http://blog.csdn.net/fanzitao/article/details/7895344

http://blog.csdn.net/taoyanqi8932/article/details/52541312

Class Solution {public:string printminnumber (vector<int> numbers) {sort (Numbers.begin (), Numbers.end (),        [] (const int& A,const int& b) {return to_string (a) +to_string (b) <to_string (b) +to_string (a);});        string res;        for (auto C:numbers) res+=to_string (c);    return res; }};

  

To rank the array into the smallest number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.