LeetCode--973。 K原点に最も近い点



Leetcode 973 K Closest Points Origin



pointsのリストがあります飛行機の中。 Kを見つけます原点に最も近い点(0, 0)

(ここでは、平面上の2点間の距離がユークリッド距離です。)



回答は任意の順序で返すことができます。答えは一意であることが保証されています(順序を除く)。

例1:



1 <= K <= points.length <= 10000

例2:

-10000

注意:

  1. -10000
  2. bool cmp(pair a,pair b){ return a.second kClosest(vector& points, int K) { vector v for(int i=0i ans for(int i=0i
  3.  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) 

意図:原点(0,0)から出力された最も近いK点を見つけることができる点のセットを与えます。



アイデア:STLコンテナーを使用し、ペアリングしてから並べ替え、最初のKポイントを取得します。

参照コード:

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