LeetCode 2857. 统计距离为 k 的点对 C#
原题链接
中等
作者:
hpstory
,
2023-09-21 16:52:17
,
所有人可见
,
阅读 71
C# 代码
public class Solution {
public int CountPairs(IList<IList<int>> coordinates, int k) {
int n = coordinates.Count;
Dictionary<(int, int), int> dict = new();
int result = 0;
for (int i = 0; i < n; i++){
int x2 = coordinates[i][0], y2 = coordinates[i][1];
for (int j = 0; j <= k; j++){
int x1 = x2 ^ j, y1 = y2 ^ (k - j);
if (dict.ContainsKey((x1, y1))) result += dict[(x1, y1)];
}
if (!dict.ContainsKey((x2, y2))) dict.Add((x2, y2), 0);
dict[(x2, y2)]++;
}
return result;
}
}