[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 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
Related Topics
Math, Divide and Conquer, Sort
Solution
解法显而易见,按照到原点的距离升序排列,取出前 K 个即可。由于 \(f(x) = \sqrt{x}\) 在其定义域上单调递增,故只需要计算根号内的 \(x\),即 \(x^2 + y^2\) 即可。
public class Solution
{
public int[][] KClosest(int[][] points, int K)
{
Array.Sort(points, delegate (int[] x, int[] y)
{
return (x[0] * x[0] + x[1] * x[1]) - (y[0] * y[0] + y[1] * y[1]);
});
int[][] ret = new int[K][];
Array.Copy(points, ret, K);
return ret;
}
}
在查看其他人的解答时,发现了如下解答(这个解答的作者的 LINQ 玩得很熟)
// no need to sqrt sinc we only want
public class Solution
{
public int[][] KClosest(int[][] points, int K)
{
return points
.OrderBy(x => x[0] * x[0] + x[1] * x[1])
.Take(K)
.ToArray();
}
}
[Solution] 973. K Closest Points to Origin的更多相关文章
- 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 ...
- 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 ...
- 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 ...
随机推荐
- CSS hack 360浏览器 极速模式与兼容模式
自动切换浏览器模式对于360浏览器7.1版本有效,8.1无效 <%@ Page Language="C#" AutoEventWireup="true" ...
- PythonStudy——Python字典底层实现原理 The underlying implementation principle of Python dictionary
在Python中,字典是通过散列表或说哈希表实现的.字典也被称为关联数组,还称为哈希数组等.也就是说,字典也是一个数组,但数组的索引是键经过哈希函数处理后得到的散列值.哈希函数的目的是使键均匀地分布在 ...
- maven+dubbo+zookeeper 基础实例
1.maven 引入依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/ ...
- Dynamics CRM Publisher
如果你想创建并且部署一个solution(解决方案),你需要创建一个publisher. 当准备创建一个solution并且创建entity field, relationship在这个solutio ...
- windows server 修改远程桌面连接端口号
1. [运行]输入 regedit 2. 在注册表编辑器中找到以下PortNamber键,改为要使用的远程端口,如10000. HKEY_LOCAL_MACHINE\SYSTEM\CurrentCo ...
- zookeeper的WEB客户端zkui使用
转载自:http://blog.csdn.net/csolo/article/details/53694665 前面几篇实践说明了zookeeper如何配置和部署,如何开发,因为大多是后台操作,对于维 ...
- std::set
std::set 不重复key 默认less排序 代码 #include <iostream> #include <set> class Person { public: ...
- NodeJS client code websocket
var WebSocketClient = require('websocket').client; var client = new WebSocketClient(); client.on('co ...
- WinForm c#操作Excel
1)Excel 的 Range 对象 在可以对 Microsoft Office Excel 2003 中的任何范围执行操作前,必须将其表示为 Range 对象并使用此 Range 的方法和属性.Ra ...
- python使用细节
python的函数位置参数在调用时可以直接传参,也可以a=5,b=7的形式传参,原以为kw参数才可以. >>> def f(a,b): print a+b >>> ...