-
Notifications
You must be signed in to change notification settings - Fork 2
/
179.LargestNumber.cpp
67 lines (60 loc) · 1.42 KB
/
179.LargestNumber.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
/*
* @lc app=leetcode id=179 lang=cpp
*
* [179] Largest Number
*
* https://leetcode.com/problems/largest-number/description/
*
* algorithms
* Medium (28.62%)
* Likes: 2152
* Dislikes: 253
* Total Accepted: 194.5K
* Total Submissions: 669.5K
* Testcase Example: '[10,2]'
*
* Given a list of non negative integers, arrange them such that they form the
* largest number.
*
* Example 1:
*
*
* Input: [10,2]
* Output: "210"
*
* Example 2:
*
*
* Input: [3,30,34,5,9]
* Output: "9534330"
*
*
* Note: The result may be very large, so you need to return a string instead
* of an integer.
*
*/
// @lc code=start
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
class Solution {
public:
std::string largestNumber(std::vector<int>& nums) {
std::vector<std::string> nums_str;
bool has_non_zero = false;
for (int num: nums) {
nums_str.push_back(std::to_string(num));
if (num && !has_non_zero) has_non_zero = true;
}
if (!has_non_zero) return "0";
std::sort(nums_str.begin(), nums_str.end(), [](const std::string& lhs, const std::string& rhs) {
if (lhs == rhs) return true;
return (lhs + rhs) > (rhs + lhs);
});
std::string result;
for (int i = 0; i < nums_str.size(); i++) result += nums_str[i];
return result;
}
};
// @lc code=end