首页 > 代码库 > leetcode 217 Contains Duplicate 数组中是否有反复的数字

leetcode 217 Contains Duplicate 数组中是否有反复的数字

??
Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.


我的解决方式:非常显然不是最优的。记录每一个插入的状态。看起来也不是非常简洁,可是对于方案二的优势是在对于长数组时候,第一个有反复的数字就退出了


class Solution {
public:
    bool containsDuplicate(vector<int>& nums)
    {
        set<int> result;
        
         set<int>::iterator itor ;

    for(int i = 0;i< nums.size();++i)
    {
        itor = result.find(nums[i]) ;

        if(itor != result.end()) 
        {
            return true;
        }
        else
        {
            result.insert(nums[i]);
        }
    }
    
    return false;
 

        
    }
};

很简洁的解决方式,类似python 了。可是stl 中的set是基于平衡树的,而python中是hash树。所以python可能会高效一些
class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        return nums.size() > set<int>(nums.begin(), nums.end()).size();        
    }
};

python 的版本号:

class Solution:
    def containsDuplicate(self, nums):
        return len(nums) > len(set(nums))


c++ 的hash版本号:同类的hash code是同样的,这是一个很重要的编程思想

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_set<int> hashset;
        for (int i = 0; i < nums.size(); ++i) {
            if (hashset.find(nums[i]) != hashset.end()) {
                return true;
            }
            else {
                hashset.insert(nums[i]);
            }
        }
        return false;
    }
};


c++排序版本号:

+2 votes
942 views
class Solution {
public:
    bool containsDuplicate(vector<int>& nums) 
    {
        int size=nums.size();
        sort(nums.begin(),nums.end());
        nums.erase(unique(nums.begin(),nums.end()),nums.end());
        return (size!=nums.size());
    }
};

+4 votes
Your running time is 28ms, if not use unique, it will be 24ms:
class Solution {
public:
    bool containsDuplicate(std::vector<int>& nums) {
        std::sort(nums.begin(), nums.end());
        for (int i = 1; i < nums.size(); ++i)
            if (nums[i] == nums[i - 1])
                return true;
        return false;
    }
};


leetcode 217 Contains Duplicate 数组中是否有反复的数字