zoukankan      html  css  js  c++  java
  • (Collection)350. Intersection of Two Arrays II

    /* Given two arrays, write a function to compute their intersection.
    
    Example:
    Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
    
    Note:
    Each element in the result should appear as many times as it shows in both arrays.
    The result can be in any order.  */
    
    
    
    public class Solution {
        public int[] intersect(int[] nums1, int[] nums2) {
         Map<Integer,Integer> map=new HashMap();
         List<Integer> list=new ArrayList();
         for(int num : nums1){
             map.put(num,map.getOrDefault(num,0)+1);
         }
         int count=0;
         for(int num : nums2){
             if(map.containsKey(num) && map.get(num)>0){
                 list.add(num);
                 map.put(num,map.get(num)-1);
             }
         }
         int[] res=new int[list.size()];
         for(int i=0;i<list.size();i++){
             res[i]=list.get(i);
         }
         return res;
        }
    }
    

      

  • 相关阅读:
    node 命令
    nodejs项目搭建
    linux 安装与配置
    GestureDetector
    activity切换效果
    hadoop
    phonegap 自定义插件
    自定义控件-属性自定义
    zxing demo
    select 语句的执行顺序
  • 原文地址:https://www.cnblogs.com/kydnn/p/5616361.html
Copyright © 2011-2022 走看看