3ms Java Solution


  • 0
    T
    public class Solution {
        public int[] intersect(int[] nums1, int[] nums2) {
            List<Integer> result = new ArrayList<Integer>();
            Arrays.sort(nums1);
            Arrays.sort(nums2);
            int next1 = 0, next2 = 0;
            while(next1 < nums1.length && next2 < nums2.length){
                if(nums1[next1] == nums2[next2]){
                    result.add(nums1[next1++]);
                    next2++;
                }
                else if(nums1[next1] > nums2[next2]){
                    next2++;
                }
                else{
                    next1++;
                }
            }
            int[] resultint = new int[result.size()];
            for(int i = 0; i<result.size(); i++){
                resultint[i] = result.get(i);
            }
            return resultint;
        }
    }
    

Log in to reply
 

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