This is my 4ms solution.please tell me the easier.


  • 0
    L

    public class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
    Arrays.sort(nums2);
    Arrays.sort(nums1);
    List<Integer> m = new ArrayList<Integer>();
    int y = 0;
    int x = 0;

    	while (x < nums2.length && y < nums1.length) {
    		if (nums1[y] == nums2[x]) {
    			m.add(nums1[y]);
    			y++;
    			x++;
    		}else if(nums1[y]<nums2[x]){
    			y++;
    		}else{
    			x++;
    		}
    	}
    	int[] sum = new int[m.size()];
    	for(int i=0;i<m.size();i++){
    		sum[i] = m.get(i).intValue();
    	}
    	return sum;
    }
    

    }


Log in to reply
 

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