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