From : https://leetcode.com/problems/contains-duplicate/
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) {
map<int, bool> mp;
int len = nums.size();
for(int i=0; i<len; i++) {
if(mp.find(nums[i])==mp.end()) {
mp[nums[i]] == true;
} else {
return true;
}
}
return false;
}
};
本文介绍了一种使用C++ map数据结构来检测数组中是否存在重复元素的方法。通过遍历数组并利用map记录每个元素出现的状态,可以高效地判断出数组是否包含重复项。
331

被折叠的 条评论
为什么被折叠?



