Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. //Runtime: 12 ms, faster than 89.15%
  2. //Memory Usage: 9.2 MB, less than 73.59%
  3.  
  4. class Solution {
  5. public:
  6. vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
  7. set<int> res;
  8. sort(nums2.begin(),nums2.end());
  9. for(auto a : nums1) {
  10. if(binarySearch(nums2,a)) {
  11. res.insert(a);
  12. }
  13. }
  14. return vector<int> (res.begin(),res.end());
  15. }
  16.  
  17. bool binarySearch(vector<int> & nums,int target) {
  18. int left = 0,right = nums.size();
  19. while (left < right) {
  20. int mid = left + (right - left) / 2;
  21. if(nums[mid] == target)
  22. return true;
  23. else if (nums[mid] < target)
  24. left = mid + 1;
  25. else
  26. right = mid;
  27. }
  28. return false;
  29. }
  30. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement