-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminimal_number.cpp
More file actions
70 lines (66 loc) · 1.72 KB
/
minimal_number.cpp
File metadata and controls
70 lines (66 loc) · 1.72 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
#include <string>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
/**
* Describe: Give a series of number, find the minimal number they can find.
*/
// The order decide compare
bool compare(const string &str1, const string &str2){
size_t idx1 = 0;
size_t idx2 = 0;
while (idx1 < str1.length() && idx2 < str2.length()) {
if (str1[idx1] > str2[idx2]) {
return true;
} else if (str1[idx1] < str2[idx2]) {
return false;
}
++idx1;
++idx2;
}
if (idx1 < str1.length()) {
string tmp = str1.substr(idx1);
return compare(tmp, str2);
}
if (idx2 < str2.length()) {
string tmp = str2.substr(idx2);
return compare(str1, tmp);
}
return false;
};
class Solution {
public:
using PQ = priority_queue<string, vector<string>, function<bool(const string &, const string &)>>;
string minNumber(vector<int>& nums) {
vector<string> strs(nums.size());
auto trans = [](int a){ return to_string(a); };
transform(nums.begin(), nums.end(), strs.begin(), trans);
PQ pq(strs.begin(), strs.end(), compare);
string rst;
while (!pq.empty()) {
rst += pq.top();
pq.pop();
}
size_t idx = 0;
while (idx < rst.length() && rst[idx] == '0') {
++idx;
}
rst = rst.substr(idx);
return rst == ""? "0" : rst;
}
};
int main() {
Solution so;
int n;
vector<int> test;
while (cin >> n) {
test = vector<int>(n);
for (auto& ele : test) {
cin >> ele;
}
auto re = so.minNumber(test);
cout << "result: " << re << endl;
}
return 0;
}