Skip to content

Commit 857be16

Browse files
authored
Merge pull request #68 from cdeepanshu/master
Added 179.cpp Leetcode problem
2 parents 8c33fc5 + 00256ff commit 857be16

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

leetcode/cpp/sort/179.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Problem Description:
3+
4+
Given a list of non-negative integers nums, arrange them such that they form the largest number.
5+
Note: The result may be very large, so you need to return a string instead of an integer.
6+
7+
Example 1:
8+
9+
Input: nums = [10,2]
10+
Output: "210"
11+
Example 2:
12+
13+
Input: nums = [3,30,34,5,9]
14+
Output: "9534330"
15+
16+
*/
17+
18+
bool compare(string a, string b)
19+
{
20+
return a+b > b+a;
21+
}
22+
class Solution {
23+
public:
24+
25+
string largestNumber(vector<int>& nums) {
26+
string ans="";
27+
vector<string>v;
28+
for(auto x :nums)
29+
{
30+
v.push_back(to_string(x));
31+
}
32+
sort(v.begin(), v.end(), compare);
33+
for(auto x :v)
34+
{
35+
ans=ans+x;
36+
}
37+
if(ans[0]=='0')
38+
{
39+
return "0";
40+
}
41+
return ans;
42+
}
43+
};

0 commit comments

Comments
 (0)