我们有一个由平面上的点组成的列表 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. centos_mysql踩坑

    1 mysql安装 a: #wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm b:rpm -ivh mysq ...

  2. MFC VC 双缓冲绘图基本原理与实现,详细解释

    转自:http://blog.csdn.net/foreverhuylee/article/details/21548107 当然你可以直接搜索到能用的代码,并且基本能满足要求.不过这样总不是学习的态 ...

  3. 如果手工启动chromedriver

    使用selenium模拟登陆网站时,有些网站会识别chrome driver里的json信息,从而判断是不是爬虫程序,做到反爬效果.(比如知乎) 下面说明下怎么手动启动chromedriver 1). ...

  4. canvas 画一条折线

    设置画布对象 canvas id="myCanvas" ref="canvas" //获取Canvas对象(画布) var canvas = document. ...

  5. Spark Streaming设计

  6. PHP算法之电话号码的字母组合

    给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:[" ...

  7. vscode编程nodejs初始安装

    nodejs官网 http://nodejs.cn/ 1.安装nodejs,记得安装时勾选配置路径 在cmd中输入node,进去node环境即为安装成功. 2.安装vscode,并安装插件node e ...

  8. SOLID设计原则

    SOLID设计原则 Single Responsibility Principle单一职责原则 单一职责原则(SRP)表明一个类有且只有一个职责. 一个类就像容器一样,它能添加任意数量的属性.方法等. ...

  9. Eureka的表兄弟Zookeeper理论基础

    Eureka的表兄弟Zookeeper 简单介绍 Zookeeper是一个开源的分布式应用程序协调服务器,主要功能包括配置维护,域名服务,分布式同步,集群管理等 主要功能简介 一.配置维护 分布式系统 ...

  10. 揭秘!2周实现上云上市,阿里云SaaS上云工具包如何打造新云梯?

    提到“上云”,很多人会理解成上IaaS,比如买一些计算.存储和网络云产品,把自己的应用系统部署上去.这的确是通常意义的上云.但对SaaS而言,需要从产品.商业.服务,三个维度考虑SaaS伙伴和客户的痛 ...