我们有一个由平面上的点组成的列表 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. PAT_A1048#Find Coins

    Source: PAT A1048 Find Coins (25 分) Description: Eva loves to collect coins from all over the univer ...

  2. PAT_A1124#Raffle for Weibo Followers

    Source: PAT A1124 Raffle for Weibo Followers (20 分) Description: John got a full mark on PAT. He was ...

  3. Mysql ibd恢复

    一,binlog恢复,这里就不说了. 二,ibd方式其实很简单, 生成数据结构(有的可以跳过) 1,创建一个新库 2,新库里新建一个表,名字和回复的表一样. 字段数量一样(字段类型和名字都无所谓) 3 ...

  4. HashMap 1.7 与 1.8 的 区别,说明 1.8 做了哪些优化,如何优化的

    JDK1.7用的链表散列结构,JDK1.8用的红黑树 在扩充HashMap的时候,JDK1.7的重新计算hash, JDK1.7只需要看看原来的hash值新增的那个bit是1还是0就好了,是0的话索引 ...

  5. this.$router.push

    跳转详情页this.$router.push({ path: `/activityDetails/${id}` })

  6. Java&Quartz实现任务调度

    目录 Java&Quartz实现任务调度 1.Quartz的作用 2.预备 3.Quartz核心 3.1.Job接口 3.2.JobDetail类 3.3 JobExecutionContex ...

  7. java内省Introspector

    大纲: JavaBean 规范 内省 一.JavaBean 规范 JavaBean —般需遵循以下规范. 实现 java.io.Serializable 接口. javaBean属性是具有getter ...

  8. SQL Server 中根据字段值查询其所在的表、字段

    DECLARE @what varchar(800)SET @what='123456' --要搜索的字符串   DECLARE @sql varchar(8000)   DECLARE TableC ...

  9. DOM——属性操作

    属性操作 非表单元素的属性 href.title.id.src.className  var link = document.getElementById('link'); console.log(l ...

  10. 阿里巴巴持续投入,etcd 正式加入 CNCF

    摘要: 2018 年 12 月 11 日,在 KubeCon + CloudNativeCon 北美峰会上,etcd 项目正式加入 CNCF. 2018 年 12 月 11 日,在 KubeCon + ...