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 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 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-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的更多相关文章
- [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 ...
- 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, ...
- LeetCode 973. K Closest Points to Origin
原题链接在这里:https://leetcode.com/problems/k-closest-points-to-origin/ 题目: We have a list of points on th ...
- 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 ...
- 【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, ...
- 【LeetCode】973. K Closest Points to Origin 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...
- [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 ...
- 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 ...
- 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 ...
随机推荐
- proxmox 安装ROS 备忘
虚拟机设置:使用qemu64 CPU和vrtio网卡在家里测试性能最好.
- cas-client单点登录客户端拦截请求和忽略/排除不需要拦截的请求URL的问题
http://blog.csdn.net/eguid_1/article/details/73611781
- 认识Excel并创建一个excel(网址:http://poi.apache.org/)
需要导入的jar包: package com.huawei.excel; import java.io.FileOutputStream; import org.apache.poi.hssf.use ...
- java 内存溢出
不健壮代码的特征及解决办法 1.尽早释放无用对象的引用.好的办法是使用临时变量的时候,让引用变量在退出活动域后,自动设置为null,暗示垃圾收集器来收集该对象,防止发生内存泄露. 对于仍然有指针指向的 ...
- CS的项目管理是基于多租户理念设计
1.创建项目 http://xxx.xxx.xx.xx:8080/client/api?command=createProject&response=json&sessionkey=8 ...
- ssdb的高可用,源码分析
ssdb,一个高性能的支持丰富数据结构的 NoSQL 数据库, 用于替代 Redis.——这是其官网的自我介绍. ssdb在leveldb存储库的基础上进行改造和丰富,添加了类似redis操作的接口, ...
- 10个android开发必备的开源项目
You are here: Home » » Blog » 10 Open Source Android Apps which every Android developer must look in ...
- ceph之image(转)
原文地址:http://www.cnblogs.com/sammyliu/p/4843812.html?utm_source=tuicool&utm_medium=referral 2 卷(i ...
- select右三角消除(转)
代码如下: select { /*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/ border: solid 1px #; /*很关键:将默认的select选择框样式清除*/ a ...
- CodeForces 346A Alice and Bob (数学最大公约数)
题意:有一堆数,然后有两个人轮流从中取出两个数,这两个数的差的绝对值不在这个集合,然后把这个数放进这个集合,如果哪个人不能拿了,就是输了,问你谁赢. 析:当时连题意都没看好,以为拿出两个数,就不放回了 ...