【发布时间】:2020-12-23 20:47:39
【问题描述】:
我正在解决 leetcode 问题(总时长可被 60 整除的歌曲对),当我将其更改为 unordered_map 并打印循环内的元素时,下面的解决方案使用了地图;元素的数量远远多于输入
class Solution {
public:
int numPairsDivisibleBy60(vector<int>& time) {
map<int, int> mod_d;
for(auto el : time) {
if(mod_d.count(el % 60) == 0) {
mod_d[el % 60] = 1;
}else mod_d[el % 60]++;
}
int ans = 0, i = 1;
//cout << "Size: " << mod_d.size() << "\n";
for(auto el : mod_d) {
int f = el.first, s = el.second;
cout << f << " " << 60 - f << "\n";
ans += mod_d[(60 - f) % 60] * (((60 - f) % 60) == f ? s - 1 : s);
}
return ans / 2;
}
};
样本输入测试:[15、63、451、213、37、209、343、319]
输出如下:
3 57
15 45
19 41
29 31
31 29
33 27
37 23
41 19
43 17
45 15
57 3
在循环内打印的元素数量应该只有 8 个,但使用 unordered_map,元素数量要多得多。
不正常的代码如下:
class Solution {
public:
int numPairsDivisibleBy60(vector<int>& time) {
unordered_map<int, int> mod_d;
for(auto el : time) {
if(mod_d.count(el % 60) == 0) {
mod_d[el % 60] = 1;
}else mod_d[el % 60]++;
}
int ans = 0, i = 1;
//cout << "Size: " << mod_d.size() << "\n";
for(auto el : mod_d) {
int f = el.first, s = el.second;
cout << f << " " << 60 - f << "\n";
ans += mod_d[(60 - f) % 60] * (((60 - f) % 60) == f ? s - 1 : s);
}
return ans / 2;
}
};
唯一的区别是使用unordered_map 而不是map
它错误地将元素打印为:
19 41
43 17
37 23
33 27
31 29
29 31
3 57
41 19
15 45
41 19
3 57
29 31
31 29
57 3
33 27
37 23
43 17
17 43
19 41
23 37
27 33
有人知道为什么会发生这种奇怪的行为吗?
【问题讨论】:
-
请显示有问题的代码,而不是没有问题的代码 (minimal reproducible example)
-
还包括输出和预期输出
-
好的,马上添加
-
第二个 for 循环在地图中创建了更多元素。根据定义,地图和无序地图以不同的顺序存储它们的内容,碰巧的是,使用 unordered_map 您最终会迭代地图中的新元素,作为同一循环的一部分。使用常规地图则不需要。
-
在第二个循环中尝试
map_d.at(...)而不是方括号,看看会发生什么
标签: c++