Solution beats 70%


  • 0
    S
    public class Solution {
        public int[] intersect(int[] nums1, int[] nums2) {
            Arrays.sort(nums1);
            Arrays.sort(nums2);
            ArrayList<Integer> result = new ArrayList<Integer>();
            
            int pt1 = 0;
            int pt2 = 0;
            
            while(pt1 < nums1.length && pt2 <nums2.length) {
                if (nums1[pt1] == nums2[pt2]) {
                    result.add(nums1[pt1]);
                    pt1++;
                    pt2++;
                }
                
                else if (nums1[pt1] < nums2[pt2]) {
                    pt1++;
                }
                else {
                    pt2++;
                }
            }
            
            int[] resArr = new int[result.size()];
            int k = 0;
            for(Integer cur: result) {
                resArr[k++] = cur;
            }
            
            return resArr;
            
        }
    }

Log in to reply
 

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