share my solution without using hashmap and hashset


  • 0
    C
    
        public int[] intersect(int[] nums1, int[] nums2) {
              Arrays.sort(nums1);
              Arrays.sort(nums2);
              int i=0;
              int j=0;
              List<Integer> list=new ArrayList<>();
              while(i<nums1.length&&j<nums2.length){
                  if(nums1[i]==nums2[j]){
                      list.add(nums1[i]);
                      i++;
                      j++;
                  }else if(nums1[i]<nums2[j]){
                      i++;
                  }else if(nums1[i]>nums2[j]){
                      j++;
                  }
              }
              int[] res=new int[list.size()];
              int p=0;
              for(int ele:list){
                  res[p++]=ele;
              }
              return res;
        }
    

Log in to reply
 

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