6ms accepted c++ solution, using two pointers


  • 0
    P
    class Solution {
    public:
        vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
            vector<int> res;
      		sort(nums1.begin(), nums1.end());
      		sort(nums2.begin(), nums2.end());
      		int i = 0, j = 0, len1 = nums1.size(), len2 = nums2.size();
      		while (i < len1 && j < len2) {
      			while (i < len1 && nums1[i] < nums2[j]) ++i;
      			while (j < len2 && nums2[j] < nums1[i]) ++j;
      			if (i == len1 || j == len2) break;
      			else if (nums1[i] == nums2[j]) {
      			    res.push_back(nums1[i]);
      			    ++i, ++j;
      			}
      		}
      		return res;
        }
    };
    

Log in to reply
 

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