File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ # 179. Largest Number
2+
3+ - Difficulty: Medium.
4+ - Related Topics: Sort.
5+ - Similar Questions: .
6+
7+ ## Problem
8+
9+ Given a list of non negative integers, arrange them such that they form the largest number.
10+
11+ ** Example 1:**
12+
13+ ```
14+ Input: [10,2]
15+ Output: "210"
16+ ```
17+
18+ ** Example 2:**
19+
20+ ```
21+ Input: [3,30,34,5,9]
22+ Output: "9534330"
23+ ```
24+
25+ ** Note:** The result may be very large, so you need to return a string instead of an integer.
26+
27+ ## Solution
28+
29+ ``` javascript
30+ /**
31+ * @param {number[]} nums
32+ * @return {string}
33+ */
34+ var largestNumber = function (nums ) {
35+ var res = nums .sort (function (a , b ) {
36+ var str1 = ' ' + a + b;
37+ var str2 = ' ' + b + a;
38+ if (str1 === str2) return 0 ;
39+ return str1 > str2 ? - 1 : 1 ;
40+ }).join (' ' );
41+ return res[0 ] === ' 0' ? ' 0' : res;
42+ };
43+ ```
44+
45+ ** Explain:**
46+
47+ nope.
48+
49+ ** Complexity:**
50+
51+ * Time complexity : O(nlog(n)).
52+ * Space complexity : O(1).
You can’t perform that action at this time.
0 commit comments