当前位置:网站首页>模拟卷Leetcode【普通】2013. 检测正方形
模拟卷Leetcode【普通】2013. 检测正方形
2022-07-15 19:54:00 【邂逅模拟卷】
汇总:模拟卷Leetcode 题解汇总
2013. 检测正方形
给你一个在 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, 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 。你可以选择:
// - 第一个,第二个,和第三个点
// - 第一个,第三个,和第四个点
提示:
point.length == 2
0 <= x, y <= 1000
调用 add 和 count 的 总次数 最多为 5000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/detect-squares
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
from leetcode_python.utils import *
class DetectSquares:
def __init__(self):
self.x2y = defaultdict(list)
self.y2x = defaultdict(list)
def add(self, point: List[int]) -> None:
x,y = point
self.x2y[x].append(y)
self.y2x[y].append(x)
def count(self, point: List[int]) -> int:
res = 0
x0,y0 = point
for x2 in self.y2x[y0]:
if x2==x0:continue
diss = x0-x2
res += self.x2y[x0].count(y0+diss) * self.x2y[x2].count(y0+diss)
res += self.x2y[x0].count(y0-diss) * self.x2y[x2].count(y0-diss)
return res
def test(data_test):
s = Solution()
data = data_test # normal
# data = [list2node(data_test[0])] # list转node
return s.getResult(*data)
def test_obj(data_test):
result = [None]
obj = DetectSquares(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[["DetectSquares","add","add","add","count","count","add","count"],[[],[[3,10]],[[11,2]],[[3,2]],[[11,10]],[[14,8]],[[11,2]],[[11,10]]]],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test_obj(data_test))
print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Codeforces Round #805 A - G
- Comprehensive explanation of e-commerce crawler API
- 我国数据安全治理研究
- Exness: crude oil stopped falling and rebounded. Pay attention to the performance of US terrorist data in the evening
- 6-Redis架构设计到使用场景-持久化机制、缓存失效策略、缓存命中率
- WTK6900H语音识别单芯片实现智能语音识别蓝牙耳机方案设计
- 基于角色的云环境下虚拟机安全访问控制策略
- 真实案例,大学生接单被骗,希望大家不要被骗了【惨痛教训】
- 构建数据基础制度,开启OID赛道新纪元
- Codeforces Round #803 (Div. 2) B. Rising Sand
猜你喜欢

Codeforces Round #802 B. Palindromic Numbers

答读者问(6):单细胞TPM矩阵如何分析?

Comprehensive explanation of e-commerce crawler API

Codeforces Round #802 A. Optimal Path

Codeforces Global Round 21 B. NIT Destroys the Universe

Scenarios that must be considered when designing a stable microservice system

LeetCode-14-最长公共前缀

LeetCode-62-不同路径

Hcip day 8 notes

The correlation between BTC and technology stocks weakened, and market uncertainty increased? 53% of investors will turn to alternative assets
随机推荐
用户登录和注册功能带验证码
构建数据基础制度,开启OID赛道新纪元
会用redis吗?那还不快来了解下redis protocol
Is it really safe for Huatai Securities to open an account?
Idea导入jar包操作以及xml file配置文件
Analysis on the application security and technology of electronic signature
Nest 框架
In a real case, college students were cheated when taking orders. I hope you won't be cheated [painful lesson]
Unity Shader——CGInclude文件cginc
华为云Stack南向开放框架,帮助生态伙伴高效入云
Cloud document management software docuware cloud how to solve five it problems
[mt2109] rectangle
Leetcode-14-longest public prefix
国信证券手机开户安全吗?开户如何办理
网络可信身份认证
LeetCode-162-寻找峰值(二分必看)
网络地址转换
Help, update to Siyuan v2.0.27, dark+ theme adaptive width problem
Legal regulation and Enlightenment of face recognition technology in the United States
华泰证券网上开户安全吗?资金有保障吗?