Skip to content

Commit 73989b4

Browse files
authored
Create sort-integers-by-the-number-of-1-bits.cpp
1 parent 3db5fa8 commit 73989b4

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Time: O(nlogn)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
vector<int> sortByBits(vector<int>& arr) {
7+
sort(begin(arr), end(arr),
8+
[](const auto& a, const auto& b) {
9+
return make_pair(__builtin_popcount(a), a) <
10+
make_pair(__builtin_popcount(b), b);
11+
});
12+
return arr;
13+
}
14+
};

0 commit comments

Comments
 (0)