-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1356.cpp
More file actions
37 lines (34 loc) · 900 Bytes
/
1356.cpp
File metadata and controls
37 lines (34 loc) · 900 Bytes
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
typedef pair<int, int> P;
class Solution {
public:
static bool compare(P& p1, P& p2) {
if (p1.first != p2.first) return p1.first < p2.first;
return p1.second < p2.second;
}
vector<int> sortByBits(vector<int>& arr) {
vector<P> v;
for (auto& num : arr) {
int cnt = __builtin_popcount(num);
v.push_back({cnt, num});
}
sort(v.begin(), v.end(), compare);
vector<int> res;
for (auto& p : v) {
res.push_back(p.second);
}
return res;
}
};
class Solution {
public:
static bool compare(int a, int b) {
int cA = __builtin_popcount(a);
int cB = __builtin_popcount(b);
if (cA == cB) return a < b;
return cA < cB;
}
vector<int> sortByBits(vector<int>& arr) {
sort(arr.begin(), arr.end(), compare);
return arr;
}
};