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

    Mono.addin是一个插件框架,更多信息请访问 http://monoaddins.codeplex.com/

  2. DEV上肤

    1,在Main中加入此语句DevExpress.UserSkins.BonusSkins.Register();SkinManager.EnableFormSkins();DevExpress.Loo ...

  3. 数组和集合(四)、Map集合的使用总结

    一.概述 键值对,无序 键唯一.值不唯一 只允许存在一个Key为null元素 二.实现类 1. HashMap · 无序,数组+链表+红黑树 · 非线程安全 2. LinkedHashMap · 有序 ...

  4. java动态规划导弹问题

    这是一道动态规划题,和昨天的取硬币还有最长公共字串有点类似. 1.题目描述:                        某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一 ...

  5. Redis学习(1)——下载与配置[转]

    Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作由VMware主 ...

  6. requests+正则表达式 爬取 妹子图

    做了一个爬取妹子图某张索引页面的爬虫,主要用request和正则表达式. 感谢 崔庆才大神的 爬虫教学视频 和 gitbook: B站:https://www.bilibili.com/video/a ...

  7. 扩展JPA方法,重写save方法

    为什么要重构save? jpa提供的save方法会将原有数据置为null,而大多数情况下我们只希望跟新自己传入的参数,所以便有了重写或者新增一个save方法. 本着解决这个问题,网上搜了很多解决方案, ...

  8. [GO]等待时间的使用

    package main import ( "time" "fmt" ) func main() { <-time.After(*time.Second) ...

  9. Spring AOP 详解 【转】

      此前对于AOP的使用仅限于声明式事务,除此之外在实际开发中也没有遇到过与之相关的问题.最近项目中遇到了以下几点需求,仔细思考之后,觉得采用AOP 来解决.一方面是为了以更加灵活的方式来解决问题,另 ...

  10. SuSE Linux上修改主机名

    1) 临时修改主机名 临时修改使用hostname即可,格式为:hostname 新主机名.Hostname命令除可以临时修改主机名外,还可以用它来查看主机名,不带参数执行它,即为查看主机名. 2)  ...