-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode_179.cpp
55 lines (48 loc) · 1.42 KB
/
leetcode_179.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
public:
bool compare(int num1, int num2){
string str1 = to_string(num1) + to_string(num2);
string str2 = to_string(num2) + to_string(num1);
double val1 = stod(str1);
double val2 = stod(str2);
return (val1 >= val2);
}
string largestNumber(vector<int>& nums) {
int n = nums.size();
for(int i = 0; i < n- 1; i++){
for (int j = 0; j < n - i - 1; j++){
if (compare(nums[j], nums[j + 1])){
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
if (nums[n - 1] == 0) return "0";
string ans = "";
for (int i =n - 1; i >= 0; i--){
ans = ans + to_string(nums[i]);
}
return ans;
}
};
class Solution {
public:
static bool compare(int num1, int num2){
string str1 = to_string(num1) + to_string(num2);
string str2 = to_string(num2) + to_string(num1);
double val1 = stod(str1);
double val2 = stod(str2);
return (val1 > val2);
}
string largestNumber(vector<int>& nums) {
int n = nums.size();
sort(nums.begin(), nums.end(), compare);
if (nums[0] == 0) return "0";
string ans = "";
for (int i = 0; i < n; i++){
ans = ans + to_string(nums[i]);
}
return ans;
}
};