leetcode.179. Largest Number

来源:互联网 发布:淘宝知识产权扣分 编辑:程序博客网 时间:2024/05/19 22:05

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.


struct {      bool operator()(string a, string b)      {          string c1 = a + b;          string c2 = b + a;          return c1 > c2;      }  } compareDigits;    class Solution {public:   string largestNumber(vector<int>& nums) {      if (nums.size() == 0) return "";          vector<string> num_str;          for (int i = 0; i < nums.size(); ++i) {                  num_str.push_back(to_string(nums[i]));          }          std::sort(num_str.begin(), num_str.end(), compareDigits);          string res1 = "";          for (int i = 0; i < num_str.size(); ++i)              res1 += num_str[i];          if (res1[0] == '0')             return "0";          else              return res1;      }     };

0 0