Skip to content

Commit 7992819

Browse files
Added solution
1 parent 5674b18 commit 7992819

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

sort_by_bits.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
vector<int> sortByBits(vector<int>& arr) {
4+
5+
6+
sort(arr.begin(), arr.end(), [](const int& A, const int &B){
7+
8+
int countA = __builtin_popcount(A), countB = __builtin_popcount(B);
9+
return countA == countB ? A < B : countA < countB;
10+
});
11+
12+
return arr;
13+
}
14+
};

0 commit comments

Comments
 (0)