The 4ms solution,where the 3ms?


  • 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;
    }
    }

  • 0
    N

    I have pretty much the same code (although not using an ArrayList, just a regular array). Having run it a few times, I get 3 and 4 ms.


  • 0
    L

    ok,I will make your code with regular array,maybe quicker!


Log in to reply
 

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