Skip to content

Latest commit

 

History

History
15 lines (15 loc) · 451 Bytes

217. Contains Duplicate.md

File metadata and controls

15 lines (15 loc) · 451 Bytes

思路

先对数组进行排序,排序后重复的数一定是相邻的,再遍历一遍即可。

C++

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