Skip to content

Commit 8da0b0d

Browse files
committed
create 0179-largest-number.cpp
1 parent a14fcbd commit 8da0b0d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

cpp/0179-largest-number.cpp

Lines changed: 29 additions & 0 deletions
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)