2013:检测正方形

This commit is contained in:
轩辕龙儿 2022-02-11 11:02:56 +08:00
parent c43acc0905
commit 1bcd0678e7
2 changed files with 163 additions and 0 deletions

View File

@ -0,0 +1,111 @@
//给你一个在 X-Y 平面上的点构成的数据流设计一个满足下述要求的算法
//
//
// 添加 一个在数据流中的新点到某个数据结构中可以添加 重复 的点并会视作不同的点进行处理
// 给你一个查询点请你从数据结构中选出三个点使这三个点和查询点一同构成一个 面积为正 轴对齐正方形 统计 满足该要求的方案数目
//
//
// 轴对齐正方形 是一个正方形除四条边长度相同外还满足每条边都与 x- y- 平行或垂直
//
// 实现 DetectSquares
//
//
// DetectSquares() 使用空数据结构初始化对象
// void add(int[] point) 向数据结构添加一个新的点 point = [x, y]
// int count(int[] point) 统计按上述方式与点 point = [x, y] 共同构造 轴对齐正方形 的方案数
//
//
//
//
// 示例
//
//
//输入
//["DetectSquares", "add", "add", "add", "count", "count", "add", "count"]
//[[], [[3, 10]], [[11, 2]], [[3, 2]], [[11, 10]], [[14, 8]], [[11, 2]], [[11, 1
//0]]]
//输出
//[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 你可以选择
// // - 第一个第二个和第三个点
// // - 第一个第三个和第四个点
//
//
//
//
// 提示
//
//
// point.length == 2
// 0 <= x, y <= 1000
// 调用 add count 总次数 最多为 5000
//
// Related Topics 设计 数组 哈希表 计数 👍 65 👎 0
package leetcode.editor.cn;
import java.util.HashMap;
import java.util.Map;
//2013:检测正方形
public class DetectSquares1 {
public static void main(String[] args) {
// Solution solution = new DetectSquares().new Solution();
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
class DetectSquares {
Map<Integer, Map<Integer, Integer>> xmap;
public DetectSquares() {
xmap = new HashMap<>();
}
public void add(int[] point) {
Map<Integer, Integer> map = xmap.getOrDefault(point[0], new HashMap<>());
map.put(point[1], map.getOrDefault(point[1], 0) + 1);
xmap.put(point[0], map);
}
public int count(int[] point) {
int x = point[0], y = point[1];
int ans = 0;
Map<Integer, Integer> col2Cnt = xmap.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 = xmap.getOrDefault(nx, new HashMap<>());
int c2 = temp.getOrDefault(y, 0), c3 = temp.getOrDefault(ny, 0);
ans += c1 * c2 * c3;
}
}
return ans;
}
}
/**
* Your DetectSquares object will be instantiated and called as such:
* DetectSquares obj = new DetectSquares();
* obj.add(point);
* int param_2 = obj.count(point);
*/
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,52 @@
<p>给你一个在 X-Y 平面上的点构成的数据流。设计一个满足下述要求的算法:</p>
<ul>
<li><strong>添加</strong> 一个在数据流中的新点到某个数据结构中<strong></strong>可以添加 <strong>重复</strong> 的点,并会视作不同的点进行处理。</li>
<li>给你一个查询点,请你从数据结构中选出三个点,使这三个点和查询点一同构成一个 <strong>面积为正</strong><strong>轴对齐正方形</strong> <strong>统计</strong> 满足该要求的方案数目<strong></strong></li>
</ul>
<p><strong>轴对齐正方形</strong> 是一个正方形,除四条边长度相同外,还满足每条边都与 x-轴 或 y-轴 平行或垂直。</p>
<p>实现 <code>DetectSquares</code> 类:</p>
<ul>
<li><code>DetectSquares()</code> 使用空数据结构初始化对象</li>
<li><code>void add(int[] point)</code> 向数据结构添加一个新的点 <code>point = [x, y]</code></li>
<li><code>int count(int[] point)</code> 统计按上述方式与点 <code>point = [x, y]</code> 共同构造 <strong>轴对齐正方形</strong> 的方案数。</li>
</ul>
<p>&nbsp;</p>
<p><strong>示例:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/09/01/image.png" style="width: 869px; height: 504px;" />
<pre>
<strong>输入:</strong>
["DetectSquares", "add", "add", "add", "count", "count", "add", "count"]
[[], [[3, 10]], [[11, 2]], [[3, 2]], [[11, 10]], [[14, 8]], [[11, 2]], [[11, 10]]]
<strong>输出:</strong>
[null, null, null, null, 1, 0, null, 2]
<strong>解释:</strong>
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 。你可以选择:
// - 第一个,第二个,和第三个点
// - 第一个,第三个,和第四个点
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>point.length == 2</code></li>
<li><code>0 &lt;= x, y &lt;= 1000</code></li>
<li>调用&nbsp;<code>add</code><code>count</code><strong>总次数</strong> 最多为 <code>5000</code></li>
</ul>
<div><div>Related Topics</div><div><li>设计</li><li>数组</li><li>哈希表</li><li>计数</li></div></div><br><div><li>👍 65</li><li>👎 0</li></div>