Two list solve this problem in Java


  • 0
    S

    public int[] intersect(int[] nums1, int[] nums2) {

        List<Integer> numList = new ArrayList();
        List<Integer> resultList = new ArrayList();
        for (int num : nums1)
        	numList.add(num);
        for (int num : nums2)
        	if (numList.contains(num)){
        		resultList.add(num);
        		numList.remove(numList.indexOf(num));
        	}
        int[] result = new int[resultList.size()];
        int index = 0;
        for (int x : resultList)
        	result[index++] = x;
        return result;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.