Skip to content

Commit d6f98d9

Browse files
committed
feat: add solutions to lcof problem: No.45
1 parent f82ca7d commit d6f98d9

File tree

7 files changed

+86
-91
lines changed

7 files changed

+86
-91
lines changed

lcof/面试题45. 把数组排成最小的数/README.md

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@
3737

3838
<!-- 这里可写通用的实现逻辑 -->
3939

40-
自定义排序比较器。
40+
**方法一:自定义排序**
41+
42+
将数组中的数字转换为字符串,然后按照字符串拼接的大小进行排序。具体地,比较两个字符串 $a$ 和 $b$,如果 $a + b \lt b + a$,则 $a$ 小于 $b$,否则 $a$ 大于 $b$。
43+
44+
时间复杂度 $O(n \times \log n + n \times m)$,空间复杂度 $O(n \times m)$。其中 $n $ 和 $m$ 分别为数组的长度和字符串的平均长度。
4145

4246
<!-- tabs:start -->
4347

@@ -46,24 +50,15 @@
4650
<!-- 这里可写当前语言的特殊实现逻辑 -->
4751

4852
```python
49-
import functools
50-
51-
5253
class Solution:
5354
def minNumber(self, nums: List[int]) -> str:
54-
if not nums:
55-
return ''
56-
57-
def compare(s1, s2):
58-
if s1 + s2 < s2 + s1:
59-
return -1
60-
if s1 + s2 > s2 + s1:
61-
return 1
62-
return 0
63-
64-
return ''.join(
65-
sorted([str(x) for x in nums], key=functools.cmp_to_key(compare))
66-
)
55+
def cmp(a, b):
56+
x, y = a + b, b + a
57+
return -1 if x < y else 1
58+
59+
ans = [str(x) for x in nums]
60+
ans.sort(key=cmp_to_key(cmp))
61+
return "".join(ans)
6762
```
6863

6964
### **Java**
@@ -73,60 +68,67 @@ class Solution:
7368
```java
7469
class Solution {
7570
public String minNumber(int[] nums) {
76-
if (nums == null || nums.length == 0) {
77-
return "";
78-
}
7971
return Arrays.stream(nums)
80-
.mapToObj(String::valueOf)
81-
.sorted((s1, s2) -> (s1 + s2).compareTo(s2 + s1))
82-
.reduce((s1, s2) -> s1 + s2)
83-
.get();
72+
.mapToObj(String::valueOf)
73+
.sorted((a, b) -> (a + b).compareTo(b + a))
74+
.reduce((a, b) -> a + b)
75+
.orElse("");
8476
}
8577
}
8678
```
8779

88-
### **JavaScript**
89-
90-
```js
91-
/**
92-
* @param {number[]} nums
93-
* @return {string}
94-
*/
95-
var minNumber = function (nums) {
96-
nums.sort((a, b) => {
97-
let s1 = a + '' + b;
98-
let s2 = b + '' + a;
99-
if (s1 < s2) {
100-
return -1;
101-
} else return 1;
102-
});
103-
return nums.join('');
104-
};
105-
```
106-
10780
### **C++**
10881

10982
```cpp
11083
class Solution {
11184
public:
11285
string minNumber(vector<int>& nums) {
113-
int n = nums.size();
114-
vector<string> strs(n);
115-
for (int i = 0; i < n; ++i) {
116-
strs[i] = to_string(nums[i]);
86+
vector<string> arr;
87+
for (int& x : nums) {
88+
arr.emplace_back(to_string(x));
11789
}
118-
sort(strs.begin(), strs.end(), [](const string& s1, const string& s2) {
119-
return s1 + s2 < s2 + s1;
90+
sort(arr.begin(), arr.end(), [](const auto& a, const auto& b) {
91+
return a + b < b + a;
12092
});
12193
string ans;
122-
for (int i = 0; i < n; ++i) {
123-
ans += strs[i];
94+
for (auto& x : arr) {
95+
ans += x;
12496
}
12597
return ans;
12698
}
12799
};
128100
```
129101
102+
### **Go**
103+
104+
```go
105+
func minNumber(nums []int) string {
106+
arr := []string{}
107+
for _, x := range nums {
108+
arr = append(arr, strconv.Itoa(x))
109+
}
110+
sort.Slice(arr, func(i, j int) bool { return arr[i]+arr[j] < arr[j]+arr[i] })
111+
return strings.Join(arr, "")
112+
}
113+
```
114+
115+
### **JavaScript**
116+
117+
```js
118+
/**
119+
* @param {number[]} nums
120+
* @return {string}
121+
*/
122+
var minNumber = function (nums) {
123+
nums.sort((a, b) => {
124+
const x = a + '' + b;
125+
const y = b + '' + a;
126+
return x < y ? -1 : 1;
127+
});
128+
return nums.join('');
129+
};
130+
```
131+
130132
### **TypeScript**
131133

132134
```ts
@@ -154,8 +156,8 @@ impl Solution {
154156
public class Solution {
155157
public string MinNumber(int[] nums) {
156158
List<string> ans = new List<string>();
157-
foreach (int temp in nums) {
158-
ans.Add(temp.ToString());
159+
foreach (int x in nums) {
160+
ans.Add(x.ToString());
159161
}
160162
ans.Sort((a, b) => (a + b).CompareTo(b + a));
161163
return string.Join("", ans);
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
class Solution {
22
public:
33
string minNumber(vector<int>& nums) {
4-
int n = nums.size();
5-
vector<string> strs(n);
6-
for (int i = 0; i < n; ++i) {
7-
strs[i] = to_string(nums[i]);
4+
vector<string> arr;
5+
for (int& x : nums) {
6+
arr.emplace_back(to_string(x));
87
}
9-
sort(strs.begin(), strs.end(), [](const string& s1, const string& s2) {
10-
return s1 + s2 < s2 + s1;
8+
sort(arr.begin(), arr.end(), [](const auto& a, const auto& b) {
9+
return a + b < b + a;
1110
});
1211
string ans;
13-
for (int i = 0; i < n; ++i) {
14-
ans += strs[i];
12+
for (auto& x : arr) {
13+
ans += x;
1514
}
1615
return ans;
1716
}
18-
};
17+
};

lcof/面试题45. 把数组排成最小的数/Solution.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
public class Solution {
22
public string MinNumber(int[] nums) {
33
List<string> ans = new List<string>();
4-
foreach (int temp in nums) {
5-
ans.Add(temp.ToString());
4+
foreach (int x in nums) {
5+
ans.Add(x.ToString());
66
}
77
ans.Sort((a, b) => (a + b).CompareTo(b + a));
88
return string.Join("", ans);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func minNumber(nums []int) string {
2+
arr := []string{}
3+
for _, x := range nums {
4+
arr = append(arr, strconv.Itoa(x))
5+
}
6+
sort.Slice(arr, func(i, j int) bool { return arr[i]+arr[j] < arr[j]+arr[i] })
7+
return strings.Join(arr, "")
8+
}
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
class Solution {
22
public String minNumber(int[] nums) {
3-
if (nums == null || nums.length == 0) {
4-
return "";
5-
}
63
return Arrays.stream(nums)
7-
.mapToObj(String::valueOf)
8-
.sorted((s1, s2) -> (s1 + s2).compareTo(s2 + s1))
9-
.reduce((s1, s2) -> s1 + s2)
10-
.get();
4+
.mapToObj(String::valueOf)
5+
.sorted((a, b) -> (a + b).compareTo(b + a))
6+
.reduce((a, b) -> a + b)
7+
.orElse("");
118
}
129
}

lcof/面试题45. 把数组排成最小的数/Solution.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
*/
55
var minNumber = function (nums) {
66
nums.sort((a, b) => {
7-
let s1 = a + '' + b;
8-
let s2 = b + '' + a;
9-
if (s1 < s2) {
10-
return -1;
11-
} else return 1;
7+
const x = a + '' + b;
8+
const y = b + '' + a;
9+
return x < y ? -1 : 1;
1210
});
1311
return nums.join('');
1412
};
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
import functools
2-
3-
41
class Solution:
52
def minNumber(self, nums: List[int]) -> str:
6-
if not nums:
7-
return ''
8-
9-
def compare(s1, s2):
10-
if s1 + s2 < s2 + s1:
11-
return -1
12-
if s1 + s2 > s2 + s1:
13-
return 1
14-
return 0
3+
def cmp(a, b):
4+
x, y = a + b, b + a
5+
return -1 if x < y else 1
156

16-
return ''.join(
17-
sorted([str(x) for x in nums], key=functools.cmp_to_key(compare))
18-
)
7+
ans = [str(x) for x in nums]
8+
ans.sort(key=cmp_to_key(cmp))
9+
return "".join(ans)

0 commit comments

Comments
 (0)