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.
1 class Solution { 2 public: 3 bool containsDuplicate(vector<int>& nums) { 4 if(nums.size()==0) return false; 5 sort(nums.begin(),nums.end()); 6 int tmp=nums[0]; 7 8 for(int i=1;i<nums.size();i++) 9 { 10 if(nums[i]==tmp) 11 return true; 12 else 13 tmp=nums[i]; 14 } 15 16 return false; 17 } 18 };
时间: 2024-10-12 17:32:35