My solution of 36ms


  • -4
    Z

    class Solution {
    public:
    bool containsDuplicate(vector<int>& nums) {
    // set<int> in_set;
    // for(int i = 0; i < nums.size(); i ++) {
    // if (in_set.find(nums[i]) != in_set.end()) {
    // return true;
    // }
    // in_set.insert(nums[i]);
    // }
    // return false;
    sort(nums.begin(), nums.end());
    for (int i = 0; i < nums.size(); i ++) {
    if (i+1 < nums.size() && nums[i] == nums[i+1])
    return true;
    }
    return false;
    }
    };


  • 0
    W

    It is too expansive to use sort, which takes O(n log(n)). Even naive implementation using a HashSet can achieve an expected time complexity of O(n).


Log in to reply
 

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