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;
}
};

LC 973. K Closest Points to Origin的更多相关文章

  1. [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 ...

  2. 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, ...

  3. LeetCode 973. K Closest Points to Origin

    原题链接在这里:https://leetcode.com/problems/k-closest-points-to-origin/ 题目: We have a list of points on th ...

  4. 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 ...

  5. 【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, ...

  6. 【LeetCode】973. K Closest Points to Origin 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...

  7. [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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 深入SpringBoot注解原理及使用

    首先,先看SpringBoot的主配置类: @SpringBootApplication public class StartEurekaApplication { public static voi ...

  2. jEasyUI 菜单与按钮

    jQuery EasyUI 菜单与按钮 – 创建简单的菜单 <div id="mm" class="easyui-menu" style="wi ...

  3. Linux磁盘的管理

    文件系统 磁盘必须要有文件系统---数据库 文件系统是用来数据存储,数据库是用来管理数据 windows fat32  ntfs   exfat linux  单文件系统 inode--索引空间(文件 ...

  4. RecyclerView item独占一行实现

    核心代码: GridLayoutManager manager = new GridLayoutManager(context, 4); manager.setSpanSizeLookup() cla ...

  5. 如何入门Pytorch之三:如何优化神经网络

    在上一节中,我们介绍了如何使用Pytorch来搭建一个经典的分类神经网络.一般情况下,搭建完模型后训练不会一次就能达到比较好的效果,这样,就需要不断的调整和优化模型的各个部分.从而引出了本文的主旨:如 ...

  6. C# 对象遍历 string类型 null转空字符串和去前后空格

    using System; using System.Collections.Generic; namespace OA.Common.Extensions { /// <summary> ...

  7. PHP面试题--基础

    1.PHP语言的一大优势是跨平台,什么是跨平台?一.PHP基础: PHP的运行环境最优搭配为Apache+MySQL+PHP,此运行环境可以在不同操作系统(例如windows.Linux等)上配置,不 ...

  8. web开发:javascript高级

    一.事件案例 二.循环绑定之变量污染 三.事件的绑定与取消 四.事件对象 一.事件案例 <!DOCTYPE html> <html> <head> <meta ...

  9. 洛谷P2341 [HAOI2006]受欢迎的牛 (Tarjan,SCC缩点)

    P2341 [HAOI2006]受欢迎的牛|[模板]强连通分量 https://www.luogu.org/problem/P2341 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就 ...

  10. sql查询数据结果发送到邮箱

    #!/bin/bash user=root password=xx dbname=xx DATE=`date +%F` #注意:此处mysql要用全路劲,否则计划任务会执行失败 /mydata/mys ...