C++ unordered_map 的一个疑问
在做一道简单题的时候,遇到了一个奇怪的问题。
LeetCode 532. K-diff Pairs in an Array
题目描述
Given an array of integers nums and an integer k, return the number of unique k-diff pairs in the array.
A k-diff pair is an integer pair (nums[i], nums[j]), where the following are true:
- 0 <= i, j < nums.length
- i != j
- |nums[i] - nums[j]| == k
- Notice that |val| denotes the absolute value of val.
Example 1:
Input: nums = [3,1,4,1,5], k = 2
Output: 2
Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of unique pairs.
Example 2:
Input: nums = [1,2,3,4,5], k = 1
Output: 4
Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
Example 3:
Input: nums = [1,3,1,5,4], k = 0
Output: 1
Explanation: There is one 0-diff pair in the array, (1, 1).
Example 4:
Input: nums = [1,2,4,4,3,3,0,9,2,3], k = 3
Output: 2
Example 5:
Input: nums = [-1,-2,-3], k = 1
Output: 2
Constraints:
- 1 <= nums.length <= 104
- -107 <= nums[i] <= 107
- 0 <= k <= 107
参考答案
class Solution {
public:
int findPairs(vector<int>& nums, int k) {
if (k < 0) return 0;
unordered_map<int,int> counter;
for (int x : nums) {
counter[x] ++;
}
int res = 0;
if (k == 0) {
for (auto&& p : counter) {
int c = p.second;
if (c > 1) {
// res += c * (c - 1) / 2;
res += 1;
}
}
} else {
for (auto&& p : counter) {
int x = p.first;
// res += counter[x] * counter[x + k];
res += (counter.count(x + k) > 0 ? 1 : 0);
}
}
return res;
}
};
疑问来了
以上代码是AC代码,但是有一份代码非常相似,却WA了。二者的不同仅仅在于把 res += (counter.count(x + k) > 0 ? 1 : 0);
换成了 res += (counter[x + k] > 0 ? 1 : 0);
,换成res += (counter[x + k] != 0 ? 1 : 0);
也同样报错。
对于题目给出的几个例子,二者都是能正确运行的,错的是以下例子:
Input:
[492,190,396,236,89,352,49,240,464,334,52,419,241,428,130,313,337,7,414,415,451,78,66,135,134,259,55,41,399,446,338,376,302,383,347,186,223,311,169,430,520,436,183,249,42,266,315,8,15,256,207,415,321,156,404,315,9,497,390,49,275,485,455,4,327,504,119,336,220,502,197,384,421,210,85,240,119,473,36,26,147,370,347,354,198,172,86,213,256,401,385,58,251,409,424,284,293,239,112,189,327,506,478,175,221,465,26,118,426,215,252,103,373,195,342,194,192,420,11,178,57,301,48,151,95,491,238,63,113,207,494,422,226,38,280,283,464,222,28,270,195,414,487,257,477,200,9,216,396,446,456,282,329,309,357,496,26,473,438,230,43,285,63,308,181,127,272,447,323,300,286,377,8,125,115,198,159,370,236,18,474,365,153,261,70,482,467,204,178,202,512,464,209,521,244,191,80,51,453,495,318,285,519,308,501,525,39,164,91,454,417,299,319,197,191,241,253,81,259,249,142,113,64,184,345,469,152,260,329,97,501,434,445,388,463,224,412,503,347,45,279,311,349,201,53,316,284,360,248,305,204,165,479,321,66,30,2,484,150,129,38,275,445,357,263,497,37,514,164,214,34,61,54,334,257,179,398,18,63,471,151,471,504,443,416,510,439,160,462,108,293,117,318,88,133,271,279,36,152,143,324,12,116,159,435,34,77,95,260,469,104,249,353,266,343,459,222,415,464,91,35,326,237,59,185,95,423,355,294,237,285,434,114,26,291,100,92,289,480,212,410,11,387,371,184,38,295,10,495,367,58,331,281,392,315,304,48,374,195,314,465,393,305,425,35,23,187,133,440,108,199,509,161,78,54,306,218,22,94,11,110,440,293,465,481,210,182,112,455,454,208,498,119,377,503,140,81,105,500,374,157,139,354,173,135,495,364,99,137,53,356,415,64,82,47,386,393,498,191,398,277,295,245,45,350,132,509,338,94,300,52,252,313,396,418,169,316,59,413,410,481,411,422,222,245,485,290,463,523,172,400,395,113,175,133,368,521,255,205,391,74,128,161,339,386,331,479,66,205,269,301,280,361,357,300,85,228,465,150,335,246,385,313,523,194,175,56,504,466,437,360,474,393,81,4,229,24,260,76,2,359,133,312,104,289,298,376,397,64,80,254,174,465,424,451,382,392,73,488,135,219,48,427,354,250,216,339,65,82,8,77,218,193,100,179,427,474,80,76,122,185,52,436,4,70,287,110,106,441,488,439,235,244,521,256,413,141,273,101,51,287,331,444,461,260,456,43,370,227,279,305,304,359,103,293,222,373,233,260,80,11,294,499,265,516,519,346,235,402,0,406,518,67,143,445,147,96,297,415,298,93,317,72,117,42,296,311,200,442,422,452,172,461,33,216,240,520,198,146,186,116,293,424,480,51,255,383,507,43,102,150,509,300,290,147,333,91,482,0,128,11,327,271,362,475,475,265,268,34,490,109,304,524,217,317,147,207,202,220,340,248,67,169,433,184,313,133,317,382,386,220,523,183,173,223,390,483,336,376,508,413,428,359,382,311,514,159,117,489,256,11,322,135,492,199,313,164,503,492,21,211,371,321,462,428,52,350,179,103,424,390,5,412,285,443,121,458,166,352,168,371,198,32,309,81,463,252,495,343,3,187,265,219,479,192,401,401,254,99,62,192,339,403,321,511,193,345,361,509,387,15,176,36,390,313,228,156,435,141,296,73,371,245,97,301,465,454,337,245,255,39,220,281,33,224,495,93,91,513,11,201,299,143,193,140,321,246,309,193,153,388,393,461,228,220,333,231,123,360,381,460,96,330,425,403,295,201,349,162,225,342,110,252,376,276,99,26,228,33,440,399,175,371,394,272,176,86,290,219,205,145,482,18,459,290,208,336,271,200,455,261,183,292,439,245,0,177,471,460,473,432,377,205,6,168,491,385,438,422,438,40,144,514,9,90,144,469,460,93,177,392,475,238,162,291,410,83,477,249,261,247,521,334,221,407,444,243,252,53,320,111,240,492,180,151,253,6,354,480,178,217,64,350,454,126,114,185,299,383,99,254,136,377,208,65,296,313,272,57,336,207,109,406,250,245,457,159,455,261,328,394,274,525,36,415,142,472,16,241,157,206,306,467,81,128,104,91,518,270,271,64,503,521,430,506,140,271,177,494,34,441,524,51,305,265,410,460,455,420,189,464,30,263,198,148,167,38,383,158,239,410,410,454,392,446,220,338,284,232,204,222,242,317,452,469,79,117,106,363,434]
196
Output
188
Expected
246
非常奇怪的问题,unordered_map 在处理不存在的key的时候,不是都返回的0么?