-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0179-largest-number.cpp
More file actions
39 lines (38 loc) · 1.05 KB
/
0179-largest-number.cpp
File metadata and controls
39 lines (38 loc) · 1.05 KB
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
#include<vector>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
class Solution {
public:
static bool customSort(const int &a, const int &b) {
if (a==0) return false;
if (b==0) return true;
int x = log10(a);
int y = log10(b);
unsigned long long p=a, q=b;
p = p * (unsigned long long)pow(10, y+1) + b;
q = q * (unsigned long long)pow(10, x+1) + a;
// cout << "input = " << a << " " << b << endl;
// cout << "comp = " << p << " " << q << endl;
return p > q;
}
string largestNumber(vector<int>& nums) {
sort(nums.begin(), nums.end(), customSort);
string ans = "";
int x, p;
for (int n:nums) {
if (n==0) {
if (ans.size()) ans += '0';
continue;
}
x = log10(n)+1;
while(x--) {
p = (int)pow(10, x);
ans += (n/p) + '0';
n = n % p;
}
}
return ans.size() ? ans:"0";
}
};