Skip to content

Commit b364705

Browse files
authored
Merge pull request #2012 from Maunish-dave/0179-largest-number
Create 0179-largest-number.cpp
2 parents 7dc41b2 + 8da0b0d commit b364705

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

cpp/0179-largest-number.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
4+
// to check which should come first
5+
// see that by adding in which way gives bigger number
6+
static bool mysort(string a, string b){
7+
return a+b > b+a;
8+
}
9+
string largestNumber(vector<int>& nums) {
10+
string s = "";
11+
vector<string> all_numbers;
12+
13+
// convert every number to string
14+
for(int it: nums){
15+
all_numbers.push_back(to_string(it));
16+
}
17+
18+
// sort accoring to custom sort function
19+
sort(all_numbers.begin(),all_numbers.end(),mysort);
20+
if(all_numbers[0]=="0"){
21+
return "0";
22+
}
23+
24+
for(string a: all_numbers){
25+
s += a;
26+
}
27+
return s;
28+
}
29+
};

0 commit comments

Comments
 (0)