Easy understanding Java code


  • 0
    H
    public int[] intersect(int[] nums1, int[] nums2) {
        List<Integer> list = new ArrayList<Integer> ();
        List<Integer> list_result = new ArrayList<Integer> ();
        for(int i=0; i<nums1.length; i++)
            list.add(nums1[i]);
        System.out.println(list.toString());
        for(int i=0; i<nums2.length ;i++) {
            if (list.contains(nums2[i])) {
                list_result.add(nums2[i]);
                list.remove(Integer.valueOf(nums2[i]));
            }
        }
        // convert arraylist to array
        int[] arr = new int[list_result.size()];
        for (int i =0; i<list_result.size(); i++)
            arr[i]=list_result.get(i);
        return arr;
    }
    

Log in to reply
 

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