题目如下:

We have a list of points on the plane.  Find the Kclosest 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

解题思路:太简单了,没啥说的。

代码如下:

class Solution(object):
def kClosest(self, points, K):
"""
:type points: List[List[int]]
:type K: int
:rtype: List[List[int]]
"""
l = []
for x,y in points:
l.append((x,y,x*x+y*y))
def cmpf(v1,v2):
return v1[2] - v2[2]
l = sorted(l,cmp=cmpf)[:K]
res = []
for x,y,z in l:
res.append([x,y])
return res

【leetcode】973. 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

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

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

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

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

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

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

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

  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. 11G利用隐含参数,修改用户名

    步骤概述: 1. 停库,修改隐含参数_enable_rename_user 为true 2. 以 restrict方式启动数据库 3.  alter user  aaaa   rename   to ...

  2. 【串线篇】实现一个RestfulCRUD

    一.概述 利用SpringMVC做一个CRUD(增删改查)符合Rest风格的: C:Create:创建 R:Retrieve:查询 U:Update:更新 D:Delete:删除 <%@tagl ...

  3. c# 7.0新语法特性

    public class NewAtturibute { public void TestFunc() { // 01 Out变量 不用初始化 "; if (int.TryParse(inp ...

  4. pic16f877a的AD实验学习

    一.主函数 //采集AD值 #include <pic.h> #include "ad.h" #include "usart.h" __CONFIG ...

  5. tcgetattr学习

    一.函数名称: int tcgetattr(int fd, struct termios *termios_p); 二.函数功能: The termios functions describe a g ...

  6. 大碗宽面Beta阶段第十一周会议记录

    本周二晚上我们在宿舍楼的大厅进行了本周的小组会议,虽然天气很冷,但大家都还是如数到场,积极参加小组会议.对于上周的任务大家都努力完成,在前端方面,大家完善了主页面和一些分页面,主要有导航栏界面的完成. ...

  7. 对象关系型数据库管理系统(PostgresQL )

    PostgresQL是   对象关系型数据库管理系统(ORDBMS).PostgreSQL支持大部分SQL标准并且提供了许多其他现代特性:复杂查询.外键.触发器.视图.事务完整性.MVCC.同样,Po ...

  8. NetHogs——Linux下按进程实时统计网络带宽利用率

    Debian/Ubuntu下安装很简单,执行:apt-get install nethogs 就可以安装. CentOS/RHEL下建议先安装上EPEL,再执行:yum install libpcap ...

  9. CJE-Jenkins认证工程师考试预约报名流程

    先决条件 考试费用150美元,需要由master/visr信用卡支付 考试全英文  哈哈哈 考试目的 通过各种渠道能够找到Jenkins的学习资料,并能够完成jenkins的配置管理,还是想全面的系统 ...

  10. tp5.0如何获取header的Authorization值

    tp5.0如何获取header的Authorization值$request->header();好像没有这个值的但是发送请求头部有的 解决方案: 在.htaccess 文件中加入 设置 Set ...