SHARE
TWEET

Untitled

bbescos Jan 27th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top