forked from Ritesh25696/interviewBit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargest Number
More file actions
38 lines (30 loc) · 934 Bytes
/
Largest Number
File metadata and controls
38 lines (30 loc) · 934 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
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 comp(string X, string Y)
{
string XY = X+Y;
string YX = Y+X;
if(XY>YX) return 1;
else return 0;
}
string Solution::largestNumber(const vector<int> &A) {
vector<string> input;
for(int i=0 ;i<A.size() ; i++){
input.push_back(to_string(A[i]));
}
string result;
sort(input.begin() , input.end(), comp);
for(int i=0 ; i<input.size() ; i++){
result+=input[i];
}
while(result[0] == '0'){
result.erase(result.begin());
}
if(result.length() == 0){
result = "0";
}
return result;
}