|
| 1 | +# [2605. Form Smallest Number From Two Digit Arrays](https://leetcode.com/problems/form-smallest-number-from-two-digit-arrays) |
| 2 | + |
| 3 | +[中文文档](/solution/2600-2699/2605.Form%20Smallest%20Number%20From%20Two%20Digit%20Arrays/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +Given two arrays of <strong>unique</strong> digits <code>nums1</code> and <code>nums2</code>, return <em>the <strong>smallest</strong> number that contains <strong>at least</strong> one digit from each array</em>. |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
| 11 | + |
| 12 | +<pre> |
| 13 | +<strong>Input:</strong> nums1 = [4,1,3], nums2 = [5,7] |
| 14 | +<strong>Output:</strong> 15 |
| 15 | +<strong>Explanation:</strong> The number 15 contains the digit 1 from nums1 and the digit 5 from nums2. It can be proven that 15 is the smallest number we can have. |
| 16 | +</pre> |
| 17 | + |
| 18 | +<p><strong class="example">Example 2:</strong></p> |
| 19 | + |
| 20 | +<pre> |
| 21 | +<strong>Input:</strong> nums1 = [3,5,2,6], nums2 = [3,1,7] |
| 22 | +<strong>Output:</strong> 3 |
| 23 | +<strong>Explanation:</strong> The number 3 contains the digit 3 which exists in both arrays. |
| 24 | +</pre> |
| 25 | + |
| 26 | +<p> </p> |
| 27 | +<p><strong>Constraints:</strong></p> |
| 28 | + |
| 29 | +<ul> |
| 30 | + <li><code>1 <= nums1.length, nums2.length <= 9</code></li> |
| 31 | + <li><code>1 <= nums1[i], nums2[i] <= 9</code></li> |
| 32 | + <li>All digits in each array are <strong>unique</strong>.</li> |
| 33 | +</ul> |
| 34 | + |
| 35 | +## Solutions |
| 36 | + |
| 37 | +<!-- tabs:start --> |
| 38 | + |
| 39 | +### **Python3** |
| 40 | + |
| 41 | +```python |
| 42 | +class Solution: |
| 43 | + def minNumber(self, nums1: List[int], nums2: List[int]) -> int: |
| 44 | + ans = 100 |
| 45 | + for a in nums1: |
| 46 | + for b in nums2: |
| 47 | + if a == b: |
| 48 | + ans = min(ans, a) |
| 49 | + else: |
| 50 | + ans = min(ans, 10 * a + b, 10 * b + a) |
| 51 | + return ans |
| 52 | +``` |
| 53 | + |
| 54 | +### **Java** |
| 55 | + |
| 56 | +```java |
| 57 | +class Solution { |
| 58 | + public int minNumber(int[] nums1, int[] nums2) { |
| 59 | + int ans = 100; |
| 60 | + for (int a : nums1) { |
| 61 | + for (int b : nums2) { |
| 62 | + if (a == b) { |
| 63 | + ans = Math.min(ans, a); |
| 64 | + } else { |
| 65 | + ans = Math.min(ans, Math.min(a * 10 + b, b * 10 + a)); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + return ans; |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +### **C++** |
| 75 | + |
| 76 | +```cpp |
| 77 | +class Solution { |
| 78 | +public: |
| 79 | + int minNumber(vector<int>& nums1, vector<int>& nums2) { |
| 80 | + int ans = 100; |
| 81 | + for (int a : nums1) { |
| 82 | + for (int b : nums2) { |
| 83 | + if (a == b) { |
| 84 | + ans = min(ans, a); |
| 85 | + } else { |
| 86 | + ans = min({ans, a * 10 + b, b * 10 + a}); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + return ans; |
| 91 | + } |
| 92 | +}; |
| 93 | +``` |
| 94 | +
|
| 95 | +### **Go** |
| 96 | +
|
| 97 | +```go |
| 98 | +func minNumber(nums1 []int, nums2 []int) int { |
| 99 | + ans := 100 |
| 100 | + for _, a := range nums1 { |
| 101 | + for _, b := range nums2 { |
| 102 | + if a == b { |
| 103 | + ans = min(ans, a) |
| 104 | + } else { |
| 105 | + ans = min(ans, min(a*10+b, b*10+a)) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + return ans |
| 110 | +} |
| 111 | +
|
| 112 | +func min(a, b int) int { |
| 113 | + if a < b { |
| 114 | + return a |
| 115 | + } |
| 116 | + return b |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +### **...** |
| 121 | + |
| 122 | +``` |
| 123 | +
|
| 124 | +``` |
| 125 | + |
| 126 | +<!-- tabs:end --> |
0 commit comments