0
点赞
收藏
分享

微信扫一扫

2013. 检测正方形 : 简单哈希表运用题


题目描述

这是 LeetCode 上的 ​​2013. 检测正方形​​ ,难度为 中等

Tag : 「哈希表」

给你一个在 X-Y 平面上的点构成的数据流。设计一个满足下述要求的算法:

  • 添加 一个在数据流中的新点到某个数据结构中。可以添加 重复 的点,并会视作不同的点进行处理。
  • 给你一个查询点,请你从数据结构中选出三个点,使这三个点和查询点一同构成一个 面积为正 的 轴对齐正方形 ,统计 满足该要求的方案数目。

轴对齐正方形 是一个正方形,除四条边长度相同外,还满足每条边都与 x-轴 或 y-轴 平行或垂直。

实现 ​​DetectSquares​​ 类:

  • ​DetectSquares()​​ 使用空数据结构初始化对象
  • ​void add(int[] point)​​​ 向数据结构添加一个新的点​​point = [x, y]​
  • ​int count(int[] point)​​​ 统计按上述方式与点​​point = [x, y]​​ 共同构造 轴对齐正方形 的方案数。

示例: 2013. 检测正方形 : 简单哈希表运用题_数据结构

输入:
["DetectSquares", "add", "add", "add", "count", "count", "add", "count"]
[[], [[3, 10]], [[11, 2]], [[3, 2]], [[11, 10]], [[14, 8]], [[11, 2]], [[11, 10]]]
输出:
[null, null, null, null, 1, 0, null, 2]

解释:
DetectSquares detectSquares = new DetectSquares();
detectSquares.add([3, 10]);
detectSquares.add([11, 2]);
detectSquares.add([3, 2]);
detectSquares.count([11, 10]); // 返回 1 。你可以选择:
// - 第一个,第二个,和第三个点
detectSquares.count([14, 8]); // 返回 0 。查询点无法与数据结构中的这些点构成正方形。
detectSquares.add([11, 2]); // 允许添加重复的点。
detectSquares.count([11, 10]); // 返回 2 。你可以选择:
// - 第一个,第二个,和第三个点
// - 第一个,第三个,和第四个点

提示:

  • 调用​​add​​​ 和​​count​​​ 的 总次数 最多为

哈希表

对于 ​​add​​​ 操作,我们可以使用「哈希表 套 哈希表」的方式,以 ​​{x, {y : 点 (x,y) 数量}}​​ 的形式对传入点进行存储。

对于 ​​count​​​ 查询而言,假定传入的点为 ,我们可以先查询 行都有哪些列,枚举这些列( 即枚举点 ),由 和 可得正方形边长 ,此时再检查唯一确定的两点 和 的出现次数,应用乘法原理,即可知道该正方形的方案数,统计所有合法方案数即是该询问的答案。

利用题目范围给定的 ​​x​​​ 和 ​​y​​​ 具有明确的范围 ​​0 <= x, y <= 1000​​,我们可以使用数组充当哈希表,但是为了拓展性和减少边界判断,即支持将平面拓展到任意大小,最好还是直接使用哈希表。

代码(数组充当哈希表代码见 ):

class DetectSquares {
Map<Integer, Map<Integer, Integer>> row2Col = new HashMap<>();

public void add(int[] point) {
int x = point[0], y = point[1];
Map<Integer, Integer> col2Cnt = row2Col.getOrDefault(x, new HashMap<>());
col2Cnt.put(y, col2Cnt.getOrDefault(y, 0) + 1);
row2Col.put(x, col2Cnt);
}

public int count(int[] point) {
int x = point[0], y = point[1];
int ans = 0;
Map<Integer, Integer> col2Cnt = row2Col.getOrDefault(x, new HashMap<>());
for (int ny : col2Cnt.keySet()) {
if (ny == y) continue;
int c1 = col2Cnt.get(ny);
int len = y - ny;
int[] nums = new int[]{x + len, x - len};
for (int nx : nums) {
Map<Integer, Integer> temp = row2Col.getOrDefault(nx, new HashMap<>());
int c2 = temp.getOrDefault(y, 0), c3 = temp.getOrDefault(ny, 0);
ans += c1 * c2 * c3;
}
}
return ans;
}
}
class DetectSquares {
int N = 1010;
int[][] cnts = new int[N][N];

public void add(int[] point) {
int x = point[0], y = point[1];
cnts[x][y]++;
}

public int count(int[] point) {
int x = point[0], y = point[1];
int ans = 0;
for (int ny = 0; ny < N; ny++) {
if (y == ny) continue;
int c1 = cnts[x][ny];
if (c1 == 0) continue;
int len = y - ny;
int[] nums = new int[]{x + len, x - len};
for (int nx : nums) {
if (nx < 0 || nx >= N) continue;
int c2 = cnts[nx][y], c3 = cnts[nx][ny];
ans += c1 * c2 * c3;
}
}
return ans;
}
}
  • 时间复杂度:​​add​​​ 操作的复杂度为,​​​count​​​ 最坏情况会扫描完所有此前加入的点,复杂度为
  • 空间复杂度:

最后

这是我们「刷穿 LeetCode」系列文章的第 ​​No.2013​​ 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:​​github.com/SharingSour…​​ 。

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。

举报

相关推荐

0 条评论