lintcode:兩數組的交 II


題目

計算兩個數組的交

 注意事項

每個元素出現次數得和在數組里一樣
答案可以以任意順序給出

樣例

nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].

解題

參考上道題,這道題只是不需要去重,直接把上道題去重部分程序去除就ok

public class Solution {
    /**
     * @param nums1 an integer array
     * @param nums2 an integer array
     * @return an integer array
     */
    public int[] intersection(int[] nums1, int[] nums2) {
        // Write your code here
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        ArrayList<Integer> A = new ArrayList<Integer>();
        int i=0;
        int j=0;
        while(i<nums1.length && j<nums2.length ){
            if(nums1[i] == nums2[j]){
                A.add(nums1[i]);
                i++;
                j++;
                
            }else if(nums1[i] < nums2[j]){
                i++;
            }else{
                j++;
            }
            
        }
        int[] res = new int[A.size()];
        for( i=0;i<A.size();i++){
            res[i] = (int)A.get(i);
        }
        return res;
    }
}

HashMap記錄數組1中相同元素出現的次數,數組2找相同,相同次數-1,為0的時候就不是交的部分了

這個HashMap是一個中間存儲,方便找到相同元素

public class Solution {
    /**
     * @param nums1 an integer array
     * @param nums2 an integer array
     * @return an integer array
     */
    public int[] intersection(int[] nums1, int[] nums2) {
        // Write your code here
        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
        
        for(int i =0;i<nums1.length;i++){
            if(!map.containsKey(nums1[i])){ // 唯一映射
                map.put(nums1[i],1);
            }else{
                map.put(nums1[i],map.get(nums1[i])+ 1 );
            }
        }
        ArrayList<Integer> A = new ArrayList<Integer>();
        for(int i=0;i<nums2.length;i++){
            if(map.containsKey(nums2[i])){ // 相同元素
                A.add(nums2[i]);
                map.put(nums2[i],map.get(nums2[i])- 1 );
                if(map.get(nums2[i])==0)
                    map.remove(nums2[i]); // 去除相同元素
            }
        }
        int[] res = new int[A.size()];
        for(int i=0;i<A.size();i++){ // 保存到數組中
            res[i] = A.get(i);
        }
        return res;
    }
}

 更新

public class Solution {
    /**
     * @param nums1 an integer array
     * @param nums2 an integer array
     * @return an integer array
     */
    public int[] intersection(int[] nums1, int[] nums2) {
        // Write your code here
        if(nums1==null || nums2 ==null)
            return new int[]{};
        HashMap<Integer,int[]> map = new HashMap<Integer,int[]>();
        List<Integer> list = new ArrayList<Integer>();
        for(int i=0;i<nums1.length;i++){
            int[] value = map.get(nums1[i]);
            if(value == null){
                map.put(nums1[i],new int[]{1});
            }else{
                value[0]++;
            }
        }
        for(int i=0;i<nums2.length;i++){
            int[] value = map.get(nums2[i]);
            if(value!=null && value[0]>=1){
                list.add(nums2[i]);
                value[0]--;
            }
        }
        int[] A = new int[list.size()];
        for(int i=0;i<list.size();i++){
            A[i] = list.get(i);
        }
        return A;
    }
}

 自定義value

public class Solution {
    /**
     * @param nums1 an integer array
     * @param nums2 an integer array
     * @return an integer array
     */
    public int[] intersection(int[] nums1, int[] nums2) {
        // Write your code here
        if(nums1==null || nums2 ==null)
            return new int[]{};
        HashMap<Integer,Value> map = new HashMap<Integer,Value>();
        List<Integer> list = new ArrayList<Integer>();
        for(int i=0;i<nums1.length;i++){
            Value value = map.get(nums1[i]);
            
            if(value == null){
                map.put(nums1[i],new Value(1));
            }else{
                int v = value.getValue();
                value.setValue(v+1);
            }
        }
        for(int i=0;i<nums2.length;i++){
            Value value = map.get(nums2[i]);
            if(value!=null){
                int v = value.getValue();
                if(v>=1){
                    list.add(nums2[i]);
                    value.setValue(v-1);    
                }
                
            }
        }
        int[] A = new int[list.size()];
        for(int i=0;i<list.size();i++){
            A[i] = list.get(i);
        }
        return A;
    }
    public class Value{
        int i;
        Value(int i){
            this.i = i;
        }
        public void setValue(int i){
            this.i = i;
        }
        public int getValue(){
            return i;
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM