Advertisement
bbescos

Untitled

Jan 27th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1.     // Run-time complexity O(nlogn)
  2.     // Memory complexity O(1)
  3.     bool containsDuplicate(vector<int>& nums) {
  4.        
  5.         if (nums.empty())
  6.             return false;
  7.        
  8.         sort(nums.begin(), nums.end());
  9.         for (int i = 0; i < nums.size() - 1; ++i) {
  10.             if (nums[i] == nums[i + 1]) {
  11.                 return true;
  12.             }
  13.         }
  14.         return false;
  15.     }
  16.    
  17.     // Run-time complexity O(n)
  18.     // Memory complexity O(n)
  19.     bool containsDuplicate(vector<int>& nums) {
  20.         unordered_set<int> hashset;
  21.         for (int num : nums) {
  22.             if (hashset.count(num))
  23.                 return true;
  24.             hashset.insert(num);
  25.         }
  26.         return false;
  27.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement