/**
5894. 至少在两个数组中出现的值
根据题目意思,数字只可能在0到100中间,所有使用定长的数组去记录每个所给的数组中数字的出现
*/
class Solution {
public List<Integer> twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) {
List<Integer> re = new ArrayList<>();
// 记录数组中出现的数
int[] slot = new int[101];
// 记录已经方如re的数
int[] mem = new int[101];
// 记录同一个数中重复出现的数
int[] sam = new int[101];
// 对应位置填充1如果存在
// 遍历数组1
for(int i = 0; i < nums1.length; i++){
slot[nums1[i]] = 1;
}
// 遍历数组2
for(int i = 0; i < nums2.length; i++){
// 在nums1中出现过
if(slot[nums2[i]] == 1 && sam[nums2[i]] == 0){
// 直接返回如果已经放入re
if(mem[nums2[i]] != 0)
continue;
// 存入符合的结果
re.add(nums2[i]);
// 做记录
mem[nums2[i]] = 1;
}else{
// 出现则记为一次
slot[nums2[i]] = 1;
// 记录nums2中该数已经出现过
sam[nums2[i]] = 1;
}
}
// 遍历nums3
for(int i = 0; i < nums3.length; i++){
// 类似第个for循环
if(slot[nums3[i]] == 1 && mem[nums3[i]] == 0){
// 存入符合结果
re.add(nums3[i]);
// 做记录
mem[nums3[i]] = 1;
}
}
return re;
}
}
数组-[5894] 至少在两个数组中出现的值
最新推荐文章于 2023-01-05 22:46:56 发布