我们有一个由平面上的点组成的列表 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. 1 <= K <= points.length <= 10000
  2. -10000 < points[i][0] < 10000
  3. -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个点的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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, ...

  4. LeetCode 973. K Closest Points to Origin

    原题链接在这里:https://leetcode.com/problems/k-closest-points-to-origin/ 题目: We have a list of points on th ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 【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, ...

随机推荐

  1. 解决 html5 input type='number' 类型可以输入e

    当给 input 设置类型为 number 时,比如,我想限制,只能输入 0-9 的正整数,正则表达式如下: /^[-]?$/ // 匹配 0-9 的整数且只匹配 0 次或 1 次 用正则测试,小数点 ...

  2. 设置非阻塞的套接字Socket

    当使用socket()函数和WSASocket()函数创建套接字时,默认都是阻塞的.在创建套接字之后,通过调用ioctlsocket()函数,将该套接字设置为非阻塞模式.函数的第一个参数是套接字,第二 ...

  3. Spark中的多线程并发处理

    Spark中的多任务处理 Spark的一个非常常见的用例是并行运行许多作业. 构建作业DAG后,Spark将这些任务分配到多个Executor上并行处理.但这并不能帮助我们在同一个Spark应用程序中 ...

  4. Python 变量作用域 LEGB (下)—— Enclosing function locals

    上篇:Python 变量作用域 LEGB (上)—— Local,Global,Builtin https://www.cnblogs.com/yvivid/p/python_LEGB_1.html ...

  5. unicode_stop - 撤销控制台unicode模式(例如, 回到8-bit模式).

    总览 unicode_stop 描述 unicode_stop 撤销以前 unicode_start(1) 命令的效果, 将显示屏和键盘设回到 8-bit 模式.

  6. git删除远程服务的文件夹

    首先查看当前分支:git branch -a 删除缓存的idea:git rm --cached -r .idea 提交gitiginore文件,将.idea从源代码仓库中删除(-m 表示注解):   ...

  7. 扩展欧几里得原理的应用:POJ1061青蛙的约会

    /* POJ 1061: 青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 123709 Accepted: 26395 ...

  8. 阿里云POLARDB如何帮助百胜软件应对数据库的“巅峰时刻”

    POLARDB是阿里云自研的下一代关系型云数据库,100%兼容MySQL,存储容量最高可达100TB,性能最高提升至MySQL的6倍,适用于企业多样化的数据库应用场景.POLARDB采用存储和计算分离 ...

  9. FontFamily 和Font 的区别

    GDI+ 将字样相同但字形不同的字体分组为字体系列.例如,下面是同一个字样(Arial),不同的字形 : ----------------------------------------------- ...

  10. NOIp2018集训test-9-6(pm)

    T1T2是洛谷原题.Orz辉神290,被辉神吊起来打. 题 1 包裹快递 二分答案.这题似乎卡精度,不开long double二分500次都过不去. //Achen #include<algor ...