The solution first checks if there is a common digit between the two arrays using set intersection. If a common digit is found, the smallest common digit is returned as the result. If there is no common digit, the solution then finds the smallest digit in each array and forms the smallest possible two-digit number from these digits. The smaller digit is placed in the tens place and the larger digit in the units place to ensure the resulting number is the smallest possible.
时间复杂度: O(n + m)
空间复杂度: O(n + m)
class Solution:
def minNumber(self, nums1: List[int], nums2: List[int]) -> int:
# Create sets from both lists to check for common digits
c = set(nums1) & set(nums2)
# If there's a common digit, return the smallest one
if c:
return min(c)
# Find the smallest digit in each list
a, b = min(nums1), min(nums2)
# Form the smallest two-digit number from a and b
if a < b:
return a * 10 + b
return b * 10 + a