-
Notifications
You must be signed in to change notification settings - Fork 56
/
LargestNumber.cpp
61 lines (36 loc) · 1.08 KB
/
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
/*
https://www.interviewbit.com/problems/largest-number/
Given a list of non negative integers, arrange them such that they form the largest number.
For example:
Given [3, 30, 34, 5, 9], the largest formed number is 9534330.
Note: The result may be very large, so you need to return a string instead of an integer.
*/
bool cmpr(string x, string y)
{
return x+y > y+x;
}
string Solution::largestNumber(const vector<int> &A)
{
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
vector<string> v;
bool zero=true;
for(auto i=0; i<A.size(); i++)
{
v.push_back(to_string(A[i]));
if(A[i] != 0)
{
zero=false;
}
}
if(zero) return "0";
sort(v.begin(), v.end(), cmpr);
string s = "";
for(auto it : v)
{
s += it;
}
return s;
}