[C + +] leetcode:128 largest number (sort by custom comparison function)

Source: Internet
Author: User

Topic:

Given a list of non negative integers, arrange them such that they form the largest number.

For example, given [3, 30, 34, 5, 9] , 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:

Greedy algorithm:

Greedy ideas: For two alternative numbers a and B, if Str (a) + str (b) > str (b) + str (b), then a before B, otherwise b before a, according to this principle of the original array from large to small order. We just need to write out a custom comparison function for the sort function, and notice that the comparison function must be a static function. And we, to note a special case, if the string after the string from 0 onwards, we return "0".

Greedy thinking is a relatively simple method, if we convert numbers into strings, and then from left to right character by case, the prefix is the same, there are many other things to consider. With a custom comparison function, all characters are compared to get the correct result.

First, we convert num into a string into an array, then sort by a custom comparison function, then concatenate the sorted string into a string, and finally exclude special cases to get the results.

Attention:

1. Note The special case if the string starts with 0 and returns "0"

2. 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. Line 26:invalid use of non-static member function

Because a non-static member function is dependent on a specific object, and the Std::sort function is global, the non-static member function cannot be called in the sort. Static member functions or global functions are independent of the object and can be accessed independently without creating any object instances. Static member functions can not invoke non-static members of the class at the same time.

Custom comparison function     static bool Compare (string &s1, String &s2)    {        return s1 + s2 > s2 + s1;    }

3. Sort comparison function

void sort (randomaccessiterator first, Randomaccessiterator last, Compare comp);
The comparison function must return an integer less than, equal to, or greater than 0, respectively, when the first argument is considered less than, equal to, or greater than the second argument. This represents a relationship of two parameters. The success returns True, and the failure returns false.

complexity: O (NLG (n)) sorting algorithm complexity

AC Code:

Class Solution {public:    string Largestnumber (vector<int> &num) {        vector<string> arr;                Converts num to string into array for        (int i:num)            arr.push_back (to_string (i));                    Sort By comparison (        Arr.begin (), arr.end (), compare);                string ret concatenated into strings        ;        for (string S:arr)            ret + = s;                Exclude Special Cases        if (ret[0] = = ' 0 ' && ret.size () > 0)             return "0";                return ret;    }    Private:    //Custom comparison function     static bool Compare (string &s1, String &s2)    {        return s1 + s2 > s2 + s1;< c21/>}};





[c++]leetcode:128 largest number (sort by custom comparison function)

Related Article

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.