-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path179_Largest Number.cpp
More file actions
71 lines (63 loc) · 2.02 KB
/
179_Largest Number.cpp
File metadata and controls
71 lines (63 loc) · 2.02 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// leetcode上面sort没法用得小心可以自己写一个
bool comparator (string &lhs, string &rhs) {
return lhs + rhs >= rhs + lhs;
}
class Solution {
public:
string largestNumber(vector<int>& nums) {
if (nums.empty()) return "";
// sort the numbers in the array
vector<string> number(nums.size());
for (int i = 0; i < nums.size(); ++i) number[i] = to_string(nums[i]);
sort(number.begin(), number.end(), comparator);
// concatenate
string ans = "";
for (string &val : number) ans += val;
// find fisrt non-0 position
int pos = 0;
while (pos < ans.size() && ans[pos] == '0') ++pos;
return pos == ans.size() ? "0" : ans.substr(pos);
}
};
// https://discuss.leetcode.com/topic/33467/c-4ms-with-explanation-o-n-log-n-time
class Solution2 {
public:
string largestNumber(vector<int>& nums) {
if (nums.size() == 0) return "";
vector<string> strs(nums.size());
for (int i = 0; i < nums.size(); ++i) strs[i] = to_string(nums[i]);
quickSort(strs, 0, strs.size() - 1);
if (strs[0] == "0") return "0";
string ans = "";
for (string val : strs) ans += val;
return ans;
}
void quickSort(vector<string>& strs, int left, int right) {
if (left >= right) return;
int pos = partition(strs, left, right);
quickSort(strs, left, pos - 1);
quickSort(strs, pos + 1, right);
return;
}
int partition(vector<string>& strs, int left, int right) {
string pivot = strs[right];
int i = left, j = right - 1;
while (i <= j) {
while ( i <= j && strs[i] + pivot >= pivot + strs[i] ) ++i;
while ( i <= j && strs[j] + pivot <= pivot + strs[j] ) --j;
if (i < j) swap(strs[i], strs[j]);
}
swap(strs[i], strs[right]);
return i;
}
};
int main() {
vector<int> nums = {3, 30, 34, 5, 9};
Solution s;
cout << s.largestNumber(nums) << endl;
return 0;
}