forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path179.cpp
More file actions
43 lines (36 loc) · 797 Bytes
/
179.cpp
File metadata and controls
43 lines (36 loc) · 797 Bytes
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
/*
Problem Description:
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"
*/
bool compare(string a, string b)
{
return a+b > b+a;
}
class Solution {
public:
string largestNumber(vector<int>& nums) {
string ans="";
vector<string>v;
for(auto x :nums)
{
v.push_back(to_string(x));
}
sort(v.begin(), v.end(), compare);
for(auto x :v)
{
ans=ans+x;
}
if(ans[0]=='0')
{
return "0";
}
return ans;
}
};