题目: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(std::vector<int>& nums) { std::set<int> s; for (int i = 0; i < nums.size(); ++i) { s.insert(nums[i]); } return s.size() != nums.size(); } };
时间: 2024-10-07 06:32:57