Last active
July 10, 2016 23:48
-
-
Save wweic/ac5947700dbc2f5ce8dac87d4fa7b2d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { | |
sort(nums1.begin(), nums1.end()); | |
sort(nums2.begin(), nums2.end()); | |
int i1 = 0, i2 = 0; | |
vector<int> result; | |
while (i1 < nums1.size() && i2 < nums2.size()) { | |
if (nums1[i1] == nums2[i2]) { | |
if (result.empty() || nums1[i1] != result.back()) { | |
result.push_back(nums1[i1]); | |
} | |
i1++; | |
i2++; | |
} else if (nums1[i1] < nums2[i2]) { | |
i1++; | |
} else { | |
i2++; | |
} | |
} | |
return result; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { | |
sort(nums1.begin(), nums1.end()); | |
sort(nums2.begin(), nums2.end()); | |
int i1 = 0, i2 = 0; | |
vector<int> result; | |
while (i1 < nums1.size() && i2 < nums2.size()) { | |
if (nums1[i1] == nums2[i2]) { | |
result.push_back(nums1[i1]); | |
while (i1 < nums1.size() && nums1[i1] == result.back()) { | |
i1++; | |
} | |
while (i2 < nums2.size() && nums2[i2] == result.back()) { | |
i2++; | |
} | |
} else if (nums1[i1] < nums2[i2]) { | |
i1++; | |
} else { | |
i2++; | |
} | |
} | |
return result; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment