We have a list of points on the plane.  Find the K closest points to the origin (0, 0).

(Here, the distance between two points on a plane is the Euclidean distance.)

You may return the answer in any order.  The answer is guaranteed to be unique (except for the order that it is in.)

Example 1:

Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].

Example 2:

Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)

Note:

  1. 1 <= K <= points.length <= 10000
  2. -10000 < points[i][0] < 10000
  3. -10000 < points[i][1] < 10000
 

Approach: native. [C++]

class Solution {
public:
vector<vector<int>> kClosest(vector<vector<int>>& points, int K) {
map<double, pair<int, int>> temp;
for (int i = 0; i < points.size(); ++i) {
double dis = sqrt(points[i][0] * points[i][0] + points[i][1] * points[i][1]);
temp[dis] = {points[i][0], points[i][1]};
}
vector<vector<int>> ans;
map<double, pair<int, int>>::iterator it;
it = temp.begin();
while (K--)
ans.push_back({it->second.first, it->second.second}), it++;
return ans;
}
};

  

973. K Closest Points to Origin的更多相关文章

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

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

  3. LeetCode 973. K Closest Points to Origin

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

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

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

  6. 【LeetCode】973. K Closest Points to Origin 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...

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

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

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

随机推荐

  1. web前端整套面试题(一)--js相关

    一.单选 1.以下哪条语句会产生运行:(A) A.var obj = ( ); B.var obj = [ ]; C.var obj = { }; D.var obj = / /; B代表数组,C代表 ...

  2. C++11之nullptr

    [C++11空指针] 1.NULL的问题 class Test { public: void TestWork(int index) { std::cout << "TestWo ...

  3. 浅析 python中的 print 和 input 的底层区别!!!

    近期的项目中 涉及到相关知识 就来总结一下 ! 先看源码: def print(self, *args, sep=' ', end='\n', file=None): # known special ...

  4. 44. Wildcard Matching 有简写的字符串匹配

    [抄题]: Given an input string (s) and a pattern (p), implement wildcard pattern matching with support ...

  5. linux系统如何安装vmware Tools(下面以CentOS为例)

    VMwareTools是VMware虚拟机中很重要的一个工具包,有些时候在虚拟机中安装完操作系统会缺少网卡驱动,不能上网,这时只要安装VMwareTools就可以解决问题,下面以CentOS为例,来说 ...

  6. Openssl pkcs12命令

    一.简介 pkcs12命令能生成和分析pkcs12文件 二.语法 openssl pkcs12 [-export] [-chain] [-inkey filename] [-certfile file ...

  7. tornado异步请求非阻塞-乾颐堂

    前言 也许有同学很迷惑:tornado不是标榜异步非阻塞解决10K问题的嘛?但是我却发现不是torando不好,而是你用错了.比如最近发现一个事情:某网站打开页面很慢,服务器cpu/内存都正常.网络状 ...

  8. Excel中使用VBA访问Access数据库

    VBA访问Access数据库 1. 通用自动化语言VBA VBA(Visual Basic For Application)是一种通用自动化语言,它可以使Excel中的常用操作自动化,还可以创建自定义 ...

  9. 电脑破解wifi密码(至少连过1次的才可以)

    电脑破解wifi密码(至少连过1次的才可以) 连过的wifi密码忘记了怎么办? 只要你电脑连过的都能破解. cmd输入以下内容查看电脑连接过的wifi名字. netsh wlan show profi ...

  10. gradle创建spring-boot项目

    刚来新公司,熟悉了公司项目搭建的框架,了解到了一种新的项目管理工具:gradle,从网上了解,据说比maven更加灵活化,于是便学习了一番.在此记录下来,一遍以后使用.gradle的安装就不说了,网上 ...