Leetcode, leetcodeoj

Source: Internet
Author: User

Leetcode, leetcodeoj

Original question:

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 is9534330.

Note: The result may be very large, so you need to return a string instead of an integer.

The best solution to this question is to sort the sequence. The key lies in the comparison function of sorting. The comparison principle is: Check that the two parameters of the comparison function follow the first and last two kinds of bills and splice them into strings. Then, the program obtains a large string Lexicographic Order.

Here, we use this question to compare the differences between the qsort function in C and the sort function in STL.

Solution 1: Use the qsort function in C.

string IntToStr(int n) {string res;if(n == 0) {res += '0';return res;}while(n) {res += n % 10 + '0';n /= 10; }reverse(res.begin(), res.end());return res;}int CMP(const void *iter1, const void *iter2) {string s1, s2;int * it1 = (int *)iter1;int * it2 = (int *)iter2;s1 = IntToStr(*it1);s2 = IntToStr(*it2);if(s1 + s2 < s2 + s1)return 1;else if(s1 + s2 > s2 + s1)return -1;elsereturn 0;}class Solution {public:    string largestNumber(vector<int> &num) {int* a = new int[num.size()];for(int i = 0; i != num.size(); i++)a[i] = num[i];//vector<int>::iterator it1 = num.begin();//vector<int>::iterator it2 = num.end();int (*cmpfunp)(const void *, const void *);cmpfunp = CMP;        qsort(a, num.size(), sizeof(int), cmpfunp);string res;for(int i = 0; i != num.size(); i++) {res += IntToStr(a[i]);}int flag = 0;for(int i = 0; i != res.size(); i++) {if(res[i] != '0'){flag = 1;break;}}if(flag == 0) {res = "0";}return res;}};
Note that void qsort (void * base, int nelem, int width, int (* fcmp) (const void *, const void *));

The four parameters are:

1. The first address of the array to be sorted 2. Number of elements to be sorted 3. space occupied by each element 4. pointer to the function, the sequence function pointer used to determine sorting must be in the form of int (* fcmp) (const void *, const void *), const cannot be small, and void cannot be changed to another type, forced type conversion can only be performed within the comparison function based on the actual comparison element type. Because qsort is a function in C, its internal mechanism is to directly operate the void type pointer, so the containers in C ++ cannot be directly sorted. This requires that when the input is vector <int>, therefore, if you want to use qsort, you can only use the int array to store the vector <int>.

When using qsort to sort other types, there are also some notes, see blog: http://www.cnblogs.com/syxchina/archive/2010/07/29/2197382.html

Solution 2: Use the sort function in STL.

string IntToStr(int n) {string res;if(n == 0) {res += '0';return res;}while(n) {res += n % 10 + '0';n /= 10; }reverse(res.begin(), res.end());return res;}bool STL_CMP(int &a, int &b) {string s1, s2;s1 = IntToStr(a);s2 = IntToStr(b);if(s1 + s2 > s2 + s1)return true;else return false;}class Solution {public:    string largestNumber(vector<int> &num) {sort(num.begin(), num.end(), STL_CMP);string res;for(int i = 0; i != num.size(); i++) {res += IntToStr(num[i]);}int flag = 0;for(int i = 0; i != res.size(); i++) {if(res[i] != '0'){flag = 1;break;}}if(flag == 0) {res = "0";}return res;}};
The sort function in STL has multiple overloaded versions. The third parameter (comparison function) can be omitted. If this parameter is omitted, a sequence arranged by operate is generated by default.

The third parameter of sort can be a function pointer (such as the above Code) or a function imitation. If it is a function pointer, the form should be bool (* cmp) (TYPE & a, TYPE & B), the return value is bool, And the qsort return value is int.

The sort and qsort here are also non-stable sorting. The Sorting Algorithm in STL also has other variants, such as stable_sort.

For details about the sort in STL, refer to the blog:

Http://blog.163.com/lovelychicken0824@126/blog/static/2277337420071894051206/

Http://www.cppblog.com/mzty/archive/2005/12/15/1770.html

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.