Leetcode973. K Closest Points to Origin最接近原点的K个点
我们有一个由平面上的点组成的列表 points。需要从中找出 K 个距离原点 (0, 0) 最近的点。
(这里,平面上两点之间的距离是欧几里德距离。)
你可以按任何顺序返回答案。除了点坐标的顺序之外,答案确保是唯一的。
示例 1:
输入:points = [[1,3],[-2,2]], K = 1 输出:[[-2,2]] 解释: (1, 3) 和原点之间的距离为 sqrt(10), (-2, 2) 和原点之间的距离为 sqrt(8), 由于 sqrt(8) < sqrt(10),(-2, 2) 离原点更近。 我们只需要距离原点最近的 K = 1 个点,所以答案就是 [[-2,2]]。
示例 2:
输入:points = [[3,3],[5,-1],[-2,4]], K = 2 输出:[[3,3],[-2,4]] (答案 [[-2,4],[3,3]] 也会被接受。)
提示:
- 1 <= K <= points.length <= 10000
- -10000 < points[i][0] < 10000
- -10000 < points[i][1] < 10000
想复杂的一种做法
struct PointNode
{
vector<int> v;
double dis;
PointNode(int x, int y)
{
v.push_back(x);
v.push_back(y);
dis = sqrt(x* x + y * y);
}
};
bool cmp(PointNode x, PointNode y)
{
return x.dis < y.dis;
}
class Solution {
public:
vector<vector<int>> kClosest(vector<vector<int>>& points, int K)
{
vector<PointNode> v;
vector<vector<int> > ans;
for (int i = 0; i < points.size(); i++)
{
v.push_back(PointNode(points[i][0], points[i][1]));
}
sort(v.begin(), v.end(), cmp);
for (int i = 0; i < K; i++)
{
ans.push_back(v[i].v);
}
return ans;
}
};
题目简单,不需要用到结构体
bool cmp(vector<int> x, vector<int> y)
{
return x[0] * x[0] + x[1] * x[1] < y[0] * y[0] + y[1] * y[1];
}
class Solution {
public:
vector<vector<int>> kClosest(vector<vector<int>>& points, int K)
{
vector<vector<int> > ans;
sort(points.begin(), points.end(), cmp);
for (int i = 0; i < K; i++)
{
ans.push_back(points[i]);
}
return ans;
}
};
Leetcode973. K Closest Points to Origin最接近原点的K个点的更多相关文章
- [Swift]LeetCode973. 最接近原点的 K 个点 | K Closest Points to Origin
We have a list of points on the plane. Find the K closest points to the origin (0, 0). (Here, the d ...
- [Solution] 973. K Closest Points to Origin
Difficulty: Easy Problem We have a list of points on the plane. Find the K closest points to the ori ...
- LeetCode 973 K Closest Points to Origin 解题报告
题目要求 We have a list of points on the plane. Find the K closest points to the origin (0, 0). (Here, ...
- LeetCode 973. K Closest Points to Origin
原题链接在这里:https://leetcode.com/problems/k-closest-points-to-origin/ 题目: We have a list of points on th ...
- 973. K Closest Points to Origin
We have a list of points on the plane. Find the K closest points to the origin (0, 0). (Here, the d ...
- 119th LeetCode Weekly Contest K Closest Points to Origin
We have a list of points on the plane. Find the K closest points to the origin (0, 0). (Here, the d ...
- LC 973. K Closest Points to Origin
We have a list of points on the plane. Find the K closest points to the origin (0, 0). (Here, the d ...
- K Closest Points to Origin
We have a list of points on the plane. Find the K closest points to the origin (0, 0). (Here, the d ...
- 【leetcode】973. K Closest Points to Origin
题目如下: We have a list of points on the plane. Find the Kclosest points to the origin (0, 0). (Here, ...
随机推荐
- JMeter AI图片识别接口并发量测试
由于临时接到一个性能测试任务,测试8个独立接口在实验室环境的TPS.响应时间以及服务器性能监控如CPU.内存.IO等,没有明确具体的响应时间与并发数,需求较模糊. 1.软件.硬件环境信息:JMeter ...
- 问题:Error running 'lugia-web': Address loaclhost:1099 is already in use
解决方法:cmd输入下面命令: 第一步: netstat -ano|findstr 1099 找到对应的pid 为3576.(每次不一样). 第二步:taskkill -f -pid 3576
- 20140814 explicit
1.explicit explicit 只对构造函数起作用,用来抑制隐式转换. 如: class A { A(int a); }; int Functi ...
- <随便写>软件设计遵循的基本原则
1.高内聚,低耦合 所谓高内聚,是指一个软件模块内各个元素彼此结合的紧密程度要高,即一个软件模块是由相关性很强的代码组成,只负责一项任务,也就是常说的单一责任原则. 所谓低耦合,是指一个软件系统内不同 ...
- python的一个简单日志记录库glog的使用
一. glog的简介 glog所记录的日志信息总是记录到标准的stderr中,即控制台终端. 每一行日志记录总是会添加一个谷歌风格的前缀,即google-style log prefix, 它的形式如 ...
- Jquery中$.get(),$.post(),$.ajax(),$.getJSON(),$.getScript(),$.load()的用法总结
参考文档 : https://blog.csdn.net/jiandanokok/article/details/48809717 本文是对Jquery中$.get(),$.post(),$.aja ...
- QT开发资料
QT开发入门资料 https://tmr.js.org/p/cc37608/ QT学习之路: https://www.devbean.net/
- 分布式项目controller项目中web.xml配置文件的编写
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" " ...
- NX二次开发-UFUN重命名工程图UF_DRAW_rename_drawing
NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_part.h> UF_initialize ...
- NX二次开发-对话框加锁UF_UI_lock_ug_access
VC/MFC调用UG Dialog要进入加锁状态 加锁 UF_UI_lock_ug_access ( UF_UI_FROM_CUSTOM ); 此处为UF_UI_select的函数 解锁 UF_UI_ ...