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 ...
随机推荐
- <转>linux操作系统编程——共享内存读写(采用信号量进行同步互斥)
http://blog.csdn.net/yanghaoran321/article/details/7872722 程序要求: 创建一个写端和一个读端,写端写入数据后读端才开始读,读端读完数据后,写 ...
- 关于Excel无法打开,因为文件格式或文件扩展名无效的解决方法
可能有网友也遇到过跟我一样的情况,新建的excel一打开就弹出 这样的错误. 这是因为excel打开的时候会去指定的文件夹找一个模板,以这个模板的样式打开新建的ecxel.所以如果excel打开的时候 ...
- Linux实战教学笔记36:PHP服务缓存加速深度优化实践
一,PHP缓存加速器介绍与环境准备 1.1 PHP缓存加速器介绍 1.1.1 操作码介绍及缓存原理 当客户端请求一个PHP程序时,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操作码(Ope ...
- mysql的my.ini配置文件
第一步,我们找到mysql安装文件下面的my.ini文件,打开可以看到第一句: # MySQL Server Instance Configuration File Mysql服务实例配置文件 好,咱 ...
- 85. Maximal Rectangle 由1拼出的最大矩形
[抄题]: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...
- CloudStack 初始化执行命令流分析
查询路由元素 选择可以使用的路由元素 需要将网络服务提供者的:虚拟路由和安全同时启用 { "createnetworkresponse" : { ...
- Oracle-属性查询
1. 查询表的部分字段属性 select t.*, c.comments from user_tab_columns t, user_col_comments c where t.table_name ...
- Siverlight MarkerSize 控制数据点半径大小 LineThickness 控制点与点之间直线的厚度
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- C++ generic tools -- from C++ Standard Library
今晚学了一下C++标准程序库, 来简单回顾和总结一下. 1.pair 结构体 // defined in <utility> , in the std namespace namespac ...
- 大前端涉猎之前后端交互总结2:使用PHP进行表单数据上传与更新
1:使用PHP进行表单上传 1.1 form表单的数据收集 HTML页面: 代码解释:核心模块是form的属性: --提交方式 : method="post" --指定 name ...