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. 1 <= K <= points.length <= 10000
  2. -10000 < points[i][0] < 10000
  3. -10000 < points[i][1] < 10000

没啥好说的,写的差劲了

struct P{
double dis;
int adr;
}H[];
bool cmd(P x,P y){
if(x.dis <= y.dis){
return ;
}
return ;
}
class Solution {
public:
vector<vector<int>> kClosest(vector<vector<int>>& points, int K) {
vector<vector<int>> x;
vector<int>y; int Len = points.size();
for(int i=;i<Len;i++){
int a = points[i][];
int b = points[i][];
//cout<<a<<" "<<b<<endl;
double result = sqrt(a*a+b*b);
H[i].dis = result;
H[i].adr = i;
}
sort(H,H+Len,cmd);
for(int i=;i<K;i++){
x.push_back(points[H[i].adr]);
}
return x;
}
};

119th LeetCode Weekly Contest K Closest Points to Origin的更多相关文章

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

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

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

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

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

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

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

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

  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. 2.sql分类

    SQL DML 和 DDL 可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL). SQL (结构化查询语言)是用于执行查询的语法.但是 SQL 语言也包含用于更新.插 ...

  2. SimpleTag——认识自定义标签

    自定义标签 自定义标签的开发与应用步骤 编写完成标签功能的 Java 类(标签处理器) 编写标签库描述(tld)文件,在tld文件中对自定义中进行描述 在 JSP 页面中导入和使用自定义标签 ①. 创 ...

  3. 远程访问Function时报错Remote table-valued function calls are not allowed.

    开始是这样调用的:select * from [LinkedServer].[db name].dbo.[function name](param1, param2) 原因: Only table-v ...

  4. linux 的各个文件夹都是干什么用

    http://www.ruanyifeng.com/blog/2012/02/a_history_of_unix_directory_structure.html http://www.pathnam ...

  5. QT背景

    Qt是一个跨平台的C++图形用户界面应用程序框架.它为应用程序开发者提供建立基于window界面所需的功能. Qt是诺基亚公司的一个产品.1996年,Qt进入商业领域,已成为全世界范围内数千种成功的应 ...

  6. 编写高质量代码改善C#程序的157个建议——建议25:谨慎集合属性的可写操作

    建议25:谨慎集合属性的可写操作 如果类型的属性中有集合属性,那么应该保证属性对象是由类型本身产生的.如果将属性设置为可写,则会增加抛出异常的几率.一般情况下,如果集合属性没有值,则它返回的Count ...

  7. 分组背包----HDU1712 ACboy needs your help

    很简单的一道分组背包入门问题.不多解释了. #include <iostream> #include <cstdio> #include <cstring> usi ...

  8. 【转】Android android listview的HeadView左右切换图片(仿新浪,网易,百度等切换图片)

    首先我们还是看一些示例:(网易,新浪,百度)      下面我简单的介绍下实现方法:其实就是listview addHeaderView.只不过这个view是一个可以切换图片的view,至于这个vie ...

  9. [database] postgresql 外网访问

    配置 环境 ubuntu 14.04 LTS 版本 postgresql version 9.3 修改监听地址 编辑 /etc/postgresql/9.3/main/postgresql.conf ...

  10. Topshelf + Quartz2.5 创建基于windows服务

    1.创建一个定时调度Quartz类 using Quartz; using Quartz.Impl; using System; using System.Collections.Generic; u ...