-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay30.cpp
57 lines (33 loc) · 1.07 KB
/
Day30.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution {
double fun(int x, int y)
{
double xx = (double)x;
double yy = (double)y;
double ans = abs((xx-0)*(xx-0) + (yy-0)*(yy-0));
ans = sqrt(ans);
return ans;
}
public:
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
vector < vector <int> > res ;
res.clear();
if(points.size()==0 or k==0) return res;
vector < pair <double, pair < int, int > > > cur;
for(auto it:points)
{
vector <int> temp = it;
int x = temp[0];
int y = temp[1];
cur.push_back( { fun(x,y), {x,y} } );
// cout << fun(x,y) <<endl;
}
sort(cur.begin(), cur.end());
for(int it = 0 ; it < k ;it++)
{
vector <int> temp = { cur[it].second.first , cur[it].second.second };
res.push_back(temp);
// cout << cur[it].first <<" : "<<cur[it].second.first <<" : "<<cur[it].second.second<<endl;
}
return res;
}
};