当前位置: 首页 > news >正文

2024.1.8每日一题

LeetCode

回旋镖的数量

447. 回旋镖的数量 - 力扣(LeetCode)

题目描述

给定平面上 n互不相同 的点 points ,其中 points[i] = [xi, yi]回旋镖 是由点 (i, j, k) 表示的元组 ,其中 ij 之间的距离和 ik 之间的欧式距离相等(需要考虑元组的顺序)。

返回平面上所有回旋镖的数量。

示例 1:

输入:points = [[0,0],[1,0],[2,0]]
输出:2
解释:两个回旋镖为 [[1,0],[0,0],[2,0]] 和 [[1,0],[2,0],[0,0]]

示例 2:

输入:points = [[1,1],[2,2],[3,3]]
输出:2

示例 3:

输入:points = [[1,1]]
输出:0

提示:

  • n == points.length
  • 1 <= n <= 500
  • points[i].length == 2
  • -104 <= xi, yi <= 104
  • 所有点都 互不相同

思路

枚举 + 哈希表

代码

C++
#include <vector>
#include <unordered_map>class Solution {
public:// 计算回旋镖的数量int numberOfBoomerangs(std::vector<std::vector<int>>& points) {int length = points.size();if (length < 3) return 0;int ans = 0;// 遍历所有点for (int i = 0; i < length; i++) {std::unordered_map<int, int> counter;// 计算距离并统计相同距离的点for (int j = 0; j < length; j++) {int dist = (points[i][0] - points[j][0]) * (points[i][0] - points[j][0]) +(points[i][1] - points[j][1]) * (points[i][1] - points[j][1]);int same = counter[dist];ans += same;counter[dist]++;}}// 每组回旋镖的数量乘以2,因为可以交换点的顺序return ans * 2;}
};
Java
class Solution {public int numberOfBoomerangs(int[][] points) {int length = points.length;if(length < 3) return 0;int ans = 0;for(int i = 0; i < length; i++){Map<Integer,Integer> counter = new HashMap<>();for(int j = 0; j < length; j++){int dist = (points[i][0] - points[j][0]) * (points[i][0] - points[j][0]) + (points[i][1] - points[j][1]) * (points[i][1] - points[j][1]);int smae = counter.getOrDefault(dist,0);ans += smae;counter.put(dist, smae + 1);}}return ans * 2;}
}

image-20240108190336005

http://www.lryc.cn/news/276023.html

相关文章:

  • 看了致远OA的表单设计后的思考
  • mmdetection训练自己的数据集
  • MySQL取出N列里最大or最小的一个数据
  • 编写.NET的Dockerfile文件构建镜像
  • 【C语言】浙大版C语言程序设计(第三版) 练习7-4 找出不是两个数组共有的元素
  • 7.27 SpringBoot项目实战 之 整合Swagger
  • 创建第一个SpringMVC项目,入手必看!
  • go 切片长度与容量的区别
  • 回归和分类区别
  • docker nginx滚动日志配置
  • 大数据分析案例-基于LinearRegression回归算法构建房屋价格预测模型
  • React-hook-form-mui(一):基本使用
  • python总结-生成器与迭代器
  • MySQL如何从数据中截取所需要的字符串
  • 动态加载和动态链接的区别
  • js数组循环,当前循环完成后执行下次循环
  • 决策树(Decision Trees)
  • 湖南大学-计算机网路-2023期末考试【部分原题回忆】
  • LCD—液晶显示
  • 论正确初始化深度学习模型参数的重要性
  • ALSA学习(5)——ASoC架构中的Machine
  • LeetCode 0447.回旋镖的数量:哈希表
  • 容器相关笔记
  • cissp 第10章 : 物理安全要求
  • 聊一聊 .NET高级调试 内核模式堆泄露
  • 海外代理IP在游戏中有什么作用?
  • 高防ip适合防御网站和游戏类的攻击吗?
  • HTML5和JS实现明媚月色效果
  • Django5+DRF序列化
  • 什么是编译程序和解释程序