[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 ...
随机推荐
- Windows7下安装python3.6.3
官网下载即可!看清自己的电脑是32还是64! 分享一个网速慢同学可下载网址:https://pan.baidu.com/s/1kU5OCOB#list/path=%2Fpub%2Fpython 1.p ...
- 20155208徐子涵《网络对抗》Exp2 后门原理与实践
20155208徐子涵<网络对抗>Exp2 后门原理与实践 基础问题回答 (1)例举你能想到的一个后门进入到你系统中的可能方式? 答:当我们在非官方网站上下载软件时,后门极有可能会进入我们 ...
- hdu 5776 抽屉定理
sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submiss ...
- linux4.15.1编译init/mounts报错
AR init/mounts.o arm-linux-ar: illegal option -- T Usage: arm-linux-ar [emulation options] [-]{dmpqr ...
- 学习笔记TF037:实现强化学习策略网络
强化学习(Reinforcement Learing),机器学习重要分支,解决连续决策问题.强化学习问题三概念,环境状态(Environment State).行动(Action).奖励(Reward ...
- ssh登录原理及免密登录方法
免密登录设置 1.进入到我的home目录 cd ~/.ssh 2.生成ssh免登陆秘钥ssh-keygen -t rsa (四个回车) 执行完这个命令后,会生成两个文件id_rsa(私钥) ...
- day11 大纲
01 昨日内容回顾 函数名的运用: 1,特殊的变量. 2,函数名可以当做变量赋值. 3,函数名可以当做容器类类型的元素. 4,函数名可以当做函数的参数. 5,函数名可以当做函数的返回值. 函数的运用: ...
- Docker概念(二)
说明: 一直想学Docker,容器.镜像.仓库什么的好难理解就没怎么看了,最近下定决心好好看看,google.Baidu发现这篇好文章,看完差不多有个大概的理解.里面的内容基本上来以下 ...
- jstack命令定位java程序CPU利用率高的代码位置
高手是怎么使用jstack精确找到异常代码的(java程序CPU利用率高的情况) 请jstack神器来帮忙 本文介绍Linux环境下使用jstack定位问题的秘笈1.[top命令]找到CPU利用率持续 ...
- 测试技术/网游Bug分析/单机修改 视频教程
早期做的一些视频,测试技术/Bug讲解/单机修改,有兴趣的同学自行下载看吧 由于是早期录制的,有口误多包涵... 链接: http://pan.baidu.com/s/1i5JUKPf 密码: a1x ...