我们有一个由平面上的点组成的列表 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. 前端(二十一)—— vue指令:文本类指令、避免页面闪烁、v-bind指令、v-on指令、v-model指令、条件渲染指令、列表渲染指令

    文本类指令.v-bind指令.v-on指令.v-model指令.条件渲染指令.列表渲染指令 一.文本操作 v-text:文本变量 <p v-text='msg'></p> &l ...

  2. JavaMail API 发送电子邮件

    现在,我们对JavaMail API及其核心类有一个清晰的概念,现在让我们写这将发送简单的电子邮件,邮件带有附件,电子邮件,HTML内容和电子邮件内嵌图像一个简单的程序. 接着在上述所有情况的基本步骤 ...

  3. Flask数据库的基本操作

    Flask操作数据库基本操作   常用的SQLAlchemy字段类型 类型名 python中类型 说明 Integer int 普通整数,一般是32位 SmallInteger int 取值范围小的整 ...

  4. 用CSS3制作50个超棒动画效果教程

    这50个CSS动画集合可以让你通过使用JavaScript函数来让动画更生动.为了能够预览到这些惊人的CSS3技术带来的动画特效,请大家使用如Safari和Chrome这类基于WebKit内核的浏览器 ...

  5. wangEditor 图片上传失败提示

    wangEditor 官网自定义上传事件:https://www.kancloud.cn/wangfupeng/wangeditor2/123689 声明:我用的wangEditor版本是2.1.23 ...

  6. http11.Http11OutputBuffer.SocketOutputBuffer.doWrite

    这是一个错误. 我在spring框架中,创建了一个基类SuperBaseController, 并且使用了@ModelAttribute用来给HttpServletRequest和HttpServle ...

  7. fedora 28 winscp链接不上

    systemctl restart sshd.service //启动sshd服务 systemctl stop firewalld //关闭防火墙 /etc/selinux/config //关闭s ...

  8. canvas 压缩图片上传

    问题:前端开发过程中难免会将数据提交到后台,但若是提交的数据过大,特别上传图片这类需求,如果不对上传的图片进行压缩处理,就难免会出现请求时间过长的情况,对于用户体验肯定就不是太友好,那么这时候该如何将 ...

  9. Development 编程规范

    { 命名规范类命名 1)所有的类名,接口名(Protocol)均以大写字母开头,多单词组合时,后面的单词首字母大写.   类,接口名必须是有意义的,切忌使用中文拼音命名.另外所有类都要加标致前缀:“O ...

  10. thinkphp 日志驱动

    日志驱动默认的命名空间位于Think\Log\Driver,驱动类需要实现的接口方法包括: 方法 说明 架构方法 __construct($config=array()) 写入方法 write($lo ...