Skip to content

Commit 3aedfb8

Browse files
authored
Create reorder-array-to-construct-the-minimum-number.cpp
1 parent 3d56942 commit 3aedfb8

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Time: O(nlogn)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
/**
7+
* @param nums n non-negative integer array
8+
* @return a string
9+
*/
10+
string minNumber(vector<int>& nums) {
11+
sort(nums.begin(), nums.end(), [](const int &i, const int &j) {
12+
return to_string(i) + to_string(j) < to_string(j) + to_string(i);
13+
});
14+
15+
string min_num;
16+
for (const auto& i : nums) {
17+
min_num.append(to_string(i));
18+
}
19+
20+
int i = 0;
21+
while (i + 1 < min_num.length() && min_num[i] == '0') {
22+
++i;
23+
}
24+
25+
return min_num.empty() ? "0" : min_num.substr(i);
26+
}
27+
};

0 commit comments

Comments
 (0)