class Solution { public: bool containsNearbyDuplicate(vector<int>& nums, int k) { unordered_map<int, int> map; // key: 数组元素, value:元素所在下表 for (int i = 0; i < nums.size(); i++) { // 找到了在索引i之前就出现过nums[i]这个元素 if (map.find(nums[i]) != map.end()) { int distance = i - map[nums[i]]; if (distance <= k) { return true; } map[nums[i]] = i; // 更新元素nums[i]所在的最新位置i } else { // 如果map里面没有,就把插入一条数据<元素,元素所在的下表> map[nums[i]] = i; } } return false; } };解释这段代码
时间: 2024-02-14 19:14:17 浏览: 87
这段代码是一个查找数组中是否存在相同元素且下标之差小于等于k的函数,函数名为containsNearbyDuplicate,参数为一个整数数组nums和一个整数k。函数的返回值为一个布尔值,表示数组中是否存在相同元素且下标之差小于等于k。
这里使用了STL中的unordered_map。unordered_map是一个无序的关联容器,它使用哈希表实现,支持快速查找、插入和删除操作。在这段代码中,unordered_map<int, int> map用于记录每个元素最近一次出现的下标。
遍历一遍数组nums,对于每个元素,如果在map中已经存在了,则说明数组中存在相同的元素,此时计算该元素最近一次出现的下标与当前下标之差是否小于等于k,如果小于等于k,则返回true,否则更新该元素在map中的下标。如果map中不存在该元素,则将该元素和当前下标插入到map中。
最后,如果遍历完数组nums后仍没有找到符合条件的元素,则返回false。
总的来说,这段代码的思路是基于unordered_map实现的,通过记录每个元素最近一次出现的下标,来判断数组中是否存在相同元素且下标之差小于等于k。
相关问题
class Solution { public: bool divideArray(vector<int>& nums) { unordered_map<int, int> freq; // 元素出现次数哈希表 for (int num: nums) { ++freq[num]; } return all_of(freq.begin(), freq.end(), [](auto p) { return p.second % 2 == 0; }); }解释这段代码
这段代码是一个判断给定整数数组中是否所有元素都是偶数的函数,函数名为divideArray,参数为一个整数数组nums。函数的返回值为一个布尔值,表示数组中是否所有元素都是偶数。
这里使用了STL中的unordered_map和all_of函数。
unordered_map是一个无序的关联容器,它使用哈希表实现,支持快速查找、插入和删除操作。在这段代码中,unordered_map<int, int> freq用于记录每个元素出现的次数。
遍历一遍数组nums,对于每个元素,将其出现次数加一。然后,使用all_of函数对freq进行遍历,判断每个元素出现的次数是否为偶数,如果是偶数则返回True,否则返回False。
all_of函数是STL中的一个函数模板,它接受一个表示区间的迭代器和一个判断条件为真的函数对象,用于判断区间中的所有元素是否都满足条件。在这段代码中,all_of(freq.begin(), freq.end(), [](auto p) { return p.second % 2 == 0; })表示遍历freq中所有元素,判断每个元素出现的次数是否为偶数。
总的来说,这段代码的思路是基于unordered_map和all_of函数实现的,通过记录每个元素出现的次数,判断数组中是否所有元素都是偶数。
class Solution { public: bool canReorderDoubled(vector<int> &arr) { unordered_map<int, int> cnt; for (int x : arr) { ++cnt[x]; } if (cnt[0] % 2) { return false; } vector<int> vals; vals.reserve(cnt.size()); for (auto &[x, _] : cnt) { vals.push_back(x); } sort(vals.begin(), vals.end(), [](int a, int b) { return abs(a) < abs(b); }); for (int x : vals) { if (cnt[2 * x] < cnt[x]) { // 无法找到足够的 2x 与 x 配对 return false; } cnt[2 * x] -= cnt[x]; } return true; } };
A) {
unordered_map<int, int> freq; // to store frequency of each number
for(int num : A) freq[num]++;
vector<int> nums; // to store unique numbers in ascending order
for(auto p : freq) nums.push_back(p.first);
sort(nums.begin(), nums.end());
for(int num : nums) {
if(freq[num] == 0) continue; // if current number is already processed, skip it
int doubleNum = num * 2; // find the double of current number
if(doubleNum == 0) { // if current number is 0, it can only be paired with itself
if(freq[num] % 2 != 0) return false; // if frequency of current number is odd, return false
freq[num] = 0; // mark current number as processed
continue;
}
if(freq[num] > freq[doubleNum]) return false; // if frequency of current number is greater than frequency of its double, return false
freq[doubleNum] -= freq[num]; // subtract frequency of current number from frequency of its double
freq[num] = 0; // mark current number as processed
}
return true; // if all numbers are processed successfully, return true
}
};
阅读全文
相关推荐

















