Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4868f8d

Browse files
committed
feat: add solutions to lc problem: No.2605
No.2605.Form Smallest Number From Two Digit Arrays
1 parent 523a4f6 commit 4868f8d

File tree

2 files changed

+388
-0
lines changed

2 files changed

+388
-0
lines changed

‎solution/2600-2699/2605.Form Smallest Number From Two Digit Arrays/README.md‎

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@
3838

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

41+
**方法一:枚举**
42+
43+
我们观察发现,如果数组 $nums1$ 和 $nums2$ 中有相同的数字,那么相同数字中的最小值一定是最小的数字。否则我们取 $nums1$ 中的数字 $a$ 和 $nums2$ 中的数字 $b,ドル将 $a$ 和 $b$ 拼接成两个数字,取其中较小的数字即可。
44+
45+
时间复杂度 $O(m \times n),ドル空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别是数组 $nums1$ 和 $nums2$ 的长度。
46+
47+
**方法二:哈希表或数组 + 枚举**
48+
49+
我们可以用哈希表或数组记录数组 $nums1$ 和 $nums2$ 中的数字,然后枚举 1ドル \sim 9,ドル如果 $i$ 在两个数组中都出现了,那么 $i$ 就是最小的数字。否则我们取 $nums1$ 中的数字 $a$ 和 $nums2$ 中的数字 $b,ドル将 $a$ 和 $b$ 拼接成两个数字,取其中较小的数字即可。
50+
51+
时间复杂度 $(m + n),ドル空间复杂度 $O(C)$。其中 $m$ 和 $n$ 分别是数组 $nums1$ 和 $nums2$ 的长度;而 $C$ 是数组 $nums1$ 和 $nums2$ 中数字的范围,本题中 $C = 10$。
52+
53+
**方法三:位运算**
54+
55+
由于数字的范围是 1ドル \sim 9,ドル我们可以用一个长度为 10ドル$ 的二进制数来表示数组 $nums1$ 和 $nums2$ 中的数字。我们用 $mask1$ 表示数组 $nums1$ 中的数字,用 $mask2$ 表示数组 $nums2$ 中的数字。
56+
57+
如果 $mask1$ 和 $mask2$ 进行按位与得到的数字 $mask$ 不等于 0ドル,ドル那么我们提取 $mask$ 中最后一位 1ドル$ 所在的位置,即为最小的数字。
58+
59+
否则,我们分别提取 $mask1$ 和 $mask2$ 中最后一位 1ドル$ 所在的位置,分别记为 $a$ 和 $b,ドル那么最小的数字就是 $min(a \times 10 + b, b \times 10 + a)$。
60+
61+
时间复杂度 $O(m + n),ドル空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别是数组 $nums1$ 和 $nums2$ 的长度。
62+
4163
<!-- tabs:start -->
4264

4365
### **Python3**
@@ -57,6 +79,32 @@ class Solution:
5779
return ans
5880
```
5981

82+
```python
83+
class Solution:
84+
def minNumber(self, nums1: List[int], nums2: List[int]) -> int:
85+
s = set(nums1) & set(nums2)
86+
if s:
87+
return min(s)
88+
a, b = min(nums1), min(nums2)
89+
return min(a * 10 + b, b * 10 + a)
90+
```
91+
92+
```python
93+
class Solution:
94+
def minNumber(self, nums1: List[int], nums2: List[int]) -> int:
95+
mask1 = mask2 = 0
96+
for x in nums1:
97+
mask1 |= 1 << x
98+
for x in nums2:
99+
mask2 |= 1 << x
100+
mask = mask1 & mask2
101+
if mask:
102+
return (mask & -mask).bit_length() - 1
103+
a = (mask1 & -mask1).bit_length() - 1
104+
b = (mask2 & -mask2).bit_length() - 1
105+
return min(a * 10 + b, b * 10 + a)
106+
```
107+
60108
### **Java**
61109

62110
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -79,6 +127,55 @@ class Solution {
79127
}
80128
```
81129

130+
```java
131+
class Solution {
132+
public int minNumber(int[] nums1, int[] nums2) {
133+
boolean[] s1 = new boolean[10];
134+
boolean[] s2 = new boolean[10];
135+
for (int x : nums1) {
136+
s1[x] = true;
137+
}
138+
for (int x : nums2) {
139+
s2[x] = true;
140+
}
141+
int a = 0, b = 0;
142+
for (int i = 1; i < 10; ++i) {
143+
if (s1[i] && s2[i]) {
144+
return i;
145+
}
146+
if (a == 0 && s1[i]) {
147+
a = i;
148+
}
149+
if (b == 0 && s2[i]) {
150+
b = i;
151+
}
152+
}
153+
return Math.min(a * 10 + b, b * 10 + a);
154+
}
155+
}
156+
```
157+
158+
```java
159+
class Solution {
160+
public int minNumber(int[] nums1, int[] nums2) {
161+
int mask1 = 0, mask2 = 0;
162+
for (int x : nums1) {
163+
mask1 |= 1 << x;
164+
}
165+
for (int x : nums2) {
166+
mask2 |= 1 << x;
167+
}
168+
int mask = mask1 & mask2;
169+
if (mask != 0) {
170+
return Integer.numberOfTrailingZeros(mask);
171+
}
172+
int a = Integer.numberOfTrailingZeros(mask1);
173+
int b = Integer.numberOfTrailingZeros(mask2);
174+
return Math.min(a * 10 + b, b * 10 + a);
175+
}
176+
}
177+
```
178+
82179
### **C++**
83180

84181
```cpp
@@ -100,6 +197,57 @@ public:
100197
};
101198
```
102199
200+
```cpp
201+
class Solution {
202+
public:
203+
int minNumber(vector<int>& nums1, vector<int>& nums2) {
204+
bitset<10> s1;
205+
bitset<10> s2;
206+
for (int x : nums1) {
207+
s1[x] = 1;
208+
}
209+
for (int x : nums2) {
210+
s2[x] = 1;
211+
}
212+
int a = 0, b = 0;
213+
for (int i = 1; i < 10; ++i) {
214+
if (s1[i] && s2[i]) {
215+
return i;
216+
}
217+
if (!a && s1[i]) {
218+
a = i;
219+
}
220+
if (!b && s2[i]) {
221+
b = i;
222+
}
223+
}
224+
return min(a * 10 + b, b * 10 + a);
225+
}
226+
};
227+
```
228+
229+
```cpp
230+
class Solution {
231+
public:
232+
int minNumber(vector<int>& nums1, vector<int>& nums2) {
233+
int mask1 = 0, mask2 = 0;
234+
for (int x : nums1) {
235+
mask1 |= 1 << x;
236+
}
237+
for (int x : nums2) {
238+
mask2 |= 1 << x;
239+
}
240+
int mask = mask1 & mask2;
241+
if (mask) {
242+
return __builtin_ctz(mask);
243+
}
244+
int a = __builtin_ctz(mask1);
245+
int b = __builtin_ctz(mask2);
246+
return min(a * 10 + b, b * 10 + a);
247+
}
248+
};
249+
```
250+
103251
### **Go**
104252
105253
```go
@@ -125,6 +273,63 @@ func min(a, b int) int {
125273
}
126274
```
127275

276+
```go
277+
func minNumber(nums1 []int, nums2 []int) int {
278+
s1 := [10]bool{}
279+
s2 := [10]bool{}
280+
for _, x := range nums1 {
281+
s1[x] = true
282+
}
283+
for _, x := range nums2 {
284+
s2[x] = true
285+
}
286+
a, b := 0, 0
287+
for i := 1; i < 10; i++ {
288+
if s1[i] && s2[i] {
289+
return i
290+
}
291+
if a == 0 && s1[i] {
292+
a = i
293+
}
294+
if b == 0 && s2[i] {
295+
b = i
296+
}
297+
}
298+
return min(a*10+b, b*10+a)
299+
}
300+
301+
func min(a, b int) int {
302+
if a < b {
303+
return a
304+
}
305+
return b
306+
}
307+
```
308+
309+
```go
310+
func minNumber(nums1 []int, nums2 []int) int {
311+
var mask1, mask2 uint
312+
for _, x := range nums1 {
313+
mask1 |= 1 << x
314+
}
315+
for _, x := range nums2 {
316+
mask2 |= 1 << x
317+
}
318+
if mask := mask1 & mask2; mask != 0 {
319+
return bits.TrailingZeros(mask)
320+
}
321+
a, b := bits.TrailingZeros(mask1), bits.TrailingZeros(mask2)
322+
return min(a*10+b, b*10+a)
323+
}
324+
325+
func min(a, b int) int {
326+
if a < b {
327+
return a
328+
}
329+
return b
330+
}
331+
```
332+
128333
### **...**
129334

130335
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /