LC.P2605[从两个数字数组里生成最小数字]

方法一:哈希表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int minNumber(int[] nums1, int[] nums2) {
boolean[] s1 = new boolean[10], s2 = new boolean[10];
for (int x : nums1) s1[x] = true;
for (int x : nums2) s2[x] = true;
int a = 0, b = 0;
for (int i = 1; i < 10; ++i) {
if (s1[i] & s2[i]) return i;
if (a == 0 && s1[i]) a = i;
if (b == 0 && s2[i]) b = i;
}
return Math.min(a * 10 + b, b * 10 + a);
}
}
  • 时间复杂度:$O(m + n)$
  • 空间复杂度:$O(C)$,其中$C = 10$