mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
16 lines
447 B
Markdown
16 lines
447 B
Markdown
![]() |
# [217. Contains Duplicate](https://leetcode.com/problems/contains-duplicate/description/)
|
||
|
# 思路
|
||
|
先对数组进行排序,排序后重复的数一定是相邻的,再遍历一遍即可。
|
||
|
# 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;
|
||
|
}
|
||
|
};
|
||
|
```
|