{"id":8911,"date":"2021-11-28T21:55:48","date_gmt":"2021-11-29T05:55:48","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8911"},"modified":"2021-11-28T21:57:19","modified_gmt":"2021-11-29T05:57:19","slug":"leetcode-179-largest-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-179-largest-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 179. Largest Number"},"content":{"rendered":"\n<p>Given a list of non-negative integers&nbsp;<code>nums<\/code>, arrange them such that they form the largest number.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;The result may be very large, so you need to return a string instead of an integer.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [10,2]\n<strong>Output:<\/strong> \"210\"\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [3,30,34,5,9]\n<strong>Output:<\/strong> \"9534330\"\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1]\n<strong>Output:<\/strong> \"1\"\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [10]\n<strong>Output:<\/strong> \"10\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 100<\/code><\/li><li><code>0 &lt;= nums[i] &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Sort numbers by lexical order.<br>e.g. 9 &gt; 666<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string largestNumber(vector<int> &num) {\n    vector<string> s;\n    for (int x : num)\n      s.emplace_back(to_string(x));\n    sort(begin(s), end(s), [](const auto& s1, const auto& s2) {\n      return s1 + s2 > s2 + s1;\n    });\n    if (s[0] == \"0\") return \"0\";\n    string ans;\n    for (int i = 0; i < s.size(); ++i)\n      ans += s[i];\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a list of non-negative integers&nbsp;nums, arrange them such that they form the largest number. Note:&nbsp;The result may be very large, so you need to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,738,4],"class_list":["post-8911","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-lexical","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8911"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8911\/revisions"}],"predecessor-version":[{"id":8913,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8911\/revisions\/8913"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}