[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 ...
随机推荐
- UVa699
这个建树的根选的很有意思,在中间作为树的根.所以二叉树建树的方法虽然一般是有两种数组的方法,一个是如果深度不太大的话,可以之间用2*k+1,2*k建树,如果很大的话,就挨着建树,弄一个结构体,有左右子 ...
- 百度地图JS只显示一个省
转载地址:http://www.cnblogs.com/wondergx/p/5305602.html 转载地址:https://blog.csdn.net/myfmyfmyfmyf/article/ ...
- Visual Studio AI环境记录(Windows10)
一.环境 Windows [版本 10.0.15063]64位 Git-2.14.1 64位[官网下载] TortoiseGit-2.5.0.0 64位[官网下载],这是一个Git 客户端,外号&qu ...
- Arch Linux root密码忘记了怎么办
https://wiki.archlinux.org/index.php/Reset_root_password_(简体中文)https://wiki.archlinux.org/index.php/ ...
- ESP8266EX资料
https://github.com/esp8266/Arduino http://espressif.com/zh-hans/support/explore/faq 电路资料图如下: 介绍功能: 参 ...
- Java 中int、String的类型转换
int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...
- Makefile中的ifeq 多条件使用
Makefile中的ifeq 多条件使用 网上关于makefile中ifeq的介绍已经很多了,为什么我还要在写这篇文章,因为他们只说了if else两种条件的情况,并没有讲多于两种条件情况的使用. 多 ...
- Spring事务原理
Spring事务的本质是对数据库事务的封装支持,没有数据库对事务的支持,Spring本身无法提供事务管理功能.对于用JDBC操作数据库想要用到事务,必须经过获取连接——>开启事务——>执行 ...
- js跳转 -- 转
第一种: 复制代码代码如下: <script language="javascript" type="text/javascript"> windo ...
- 给idea添加类注释和方法注释模板
这是我找到的最好的,最简单明白的一文: https://blog.csdn.net/xiaoliulang0324/article/details/79030752