We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a14fcbd commit 8da0b0dCopy full SHA for 8da0b0d
cpp/0179-largest-number.cpp
@@ -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