-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path179.largest-number.cpp
85 lines (82 loc) · 1.46 KB
/
179.largest-number.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* @lc app=leetcode id=179 lang=cpp
*
* [179] Largest Number
*
* https://leetcode.com/problems/largest-number/description/
*
* algorithms
* Medium (30.34%)
* Likes: 2830
* Dislikes: 302
* Total Accepted: 238.6K
* Total Submissions: 778.1K
* Testcase Example: '[10,2]'
*
* Given a list of non-negative integers nums, arrange them such that they form
* the largest number.
*
* Note: The result may be very large, so you need to return a string instead
* of an integer.
*
*
* Example 1:
*
*
* Input: nums = [10,2]
* Output: "210"
*
*
* Example 2:
*
*
* Input: nums = [3,30,34,5,9]
* Output: "9534330"
*
*
* Example 3:
*
*
* Input: nums = [1]
* Output: "1"
*
*
* Example 4:
*
*
* Input: nums = [10]
* Output: "10"
*
*
*
* Constraints:
*
*
* 1 <= nums.length <= 100
* 0 <= nums[i] <= 10^9
*
*
*/
// @lc code=start
class Solution {
public:
string largestNumber(vector<int>& nums) {
auto cmp = [](string& s1, string& s2) {
string&& L1 = s1 + s2;
string&& L2 = s2 + s1;
return L1 > L2;
};
string result;
vector<string> strs;
for (const auto& ele : nums) {
strs.push_back(to_string(ele));
}
sort(strs.begin(), strs.end(), cmp);
if (strs[0] == "0")
return "0";
for (const auto& s : strs)
result += s;
return result;
}
};
// @lc code=end