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.)
class Solution {
public:
static bool cmp(vector<long long> a, vector<long long> b){
return a[] < b[];
}
vector<vector<int>> kClosest(vector<vector<int>>& points, int K) {
vector<vector<int>> ret;
vector<vector<long long>> dist;
for(auto pv : points) {
vector<long long> tmp = {pv[], pv[], pv[]*pv[] + pv[]*pv[]};
dist.push_back(tmp);
}
sort(dist.begin(), dist.end(), cmp);
for(int i=; i<K; i++){
vector<int> tmp = {(int)dist[i][], (int)dist[i][]};
ret.push_back(tmp);
}
return ret;
}
};

最新文章

  1. ELK+Kafka集群日志分析系统
  2. django学习&lt;一&gt;:安装
  3. checkbox选中与取消选择
  4. jackson反序列化时忽略不需要的字段
  5. 在VS2010中ActiveX控件注册方法,使用regsvr32命令
  6. a^b-b^a - SGU 112(高精度快速幂)
  7. Oracle EBS-SQL (BOM-1):检查供应类型错误.sql
  8. java内存模型6-final
  9. Python资料汇总(建议收藏)
  10. 流API--提取流+组合流
  11. python:前端(HTML)+后端(Django)+数据库(MySQL)
  12. JavaScript02-js使用
  13. 学号 20175329 2018-2019-3《Java程序设计》第九周学习总结
  14. 理解java关键字Synchronized(学习笔记)
  15. Python学习笔记【第九篇】:Python面向对象基础
  16. 【PyTorch深度学习60分钟快速入门 】Part1:PyTorch是什么?
  17. CCPC-Wannafly Winter Camp Day7 D---二次函数【数论】【构造】
  18. Mysql去掉html标签函数
  19. Bootstrap之Bootstrap组件
  20. RDM 使用与破解

热门文章

  1. LINUX 使用grep命令查看某个指定时间段的日志
  2. 手动编译用于i.MX6系列ARM的交叉编译SDK
  3. 5.安装bacula-web(监控页面)
  4. TensorFlow中numpy与tensor数据相互转化
  5. Java&amp;Selenium&amp;JS&amp;AWT之那些难以点击到的按钮
  6. Sleep-Join方法理解
  7. 关于微信小程序的本地存储
  8. MyBatis-10-多对一处理
  9. hbase实践之写流程
  10. Codeforces工具总结