Input: nums1 =[4,1,3], nums2 =[5,7]Output: 15Explanation: The number 15 contains the digit 1 from nums1 and the digit 5 from nums2. It can be proven that 15is the smallest number we can have.
classSolution {
public:int minNumber(vector<int>& nums1, vector<int>& nums2) {
int ans =100;
for (int x : nums1)
for (int y : nums2)
if (x == y) ans = min(ans, x);
else ans = min(ans, min(x *10+ y, y *10+ x));
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
typeSolutionstruct{}
func (Solution) minNumber(nums1, nums2 []int) int {
ans:=100for_, x:=rangenums1 {
for_, y:=rangenums2 {
ifx==y {
ifx < ans { ans = x }
} else {
ifx*10+y < ans { ans = x*10+y }
ify*10+x < ans { ans = y*10+x }
}
}
}
returnans}
1
2
3
4
5
6
7
8
9
10
classSolution {
publicintminNumber(int[] nums1, int[] nums2) {
int ans = 100;
for (int x : nums1)
for (int y : nums2)
if (x == y) ans = Math.min(ans, x);
else ans = Math.min(ans, Math.min(x * 10 + y, y * 10 + x));
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
funminNumber(nums1: IntArray, nums2: IntArray): Int {
var ans = 100for (x in nums1) {
for (y in nums2) {
if (x == y) ans = minOf(ans, x)
else ans = minOf(ans, x * 10 + y, y * 10 + x)
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
classSolution:
defminNumber(self, nums1: list[int], nums2: list[int]) -> int:
ans =100for x in nums1:
for y in nums2:
if x == y:
ans = min(ans, x)
else:
ans = min(ans, x *10+ y, y *10+ x)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
impl Solution {
pubfnmin_number(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
letmut ans =100;
for&x in&nums1 {
for&y in&nums2 {
if x == y {
ans = ans.min(x);
} else {
ans = ans.min(x *10+ y).min(y *10+ x);
}
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
minNumber(nums1: number[], nums2: number[]):number {
letans=100;
for (constxofnums1) {
for (constyofnums2) {
if (x===y) ans= Math.min(ans, x);
elseans= Math.min(ans, x*10+y, y*10+x);
}
}
returnans;
}
}