数组-[5894] 至少在两个数组中出现的值

/**
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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值