Skip to content

Commit

Permalink
1356
Browse files Browse the repository at this point in the history
  • Loading branch information
isinsuarici committed Dec 11, 2022
1 parent cfea55b commit b176ab2
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions E_1356_SortIntegersbyTheNumberof1Bits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:
vector<int> sortByBits(vector<int>& arr) {
multiset<pair<int,int>> ms;
for (int i =0; i<arr.size(); i++){
int num1s = numof1(arr[i]);
ms.insert({num1s,arr[i]});
}
vector<int> res;
for(auto el: ms){
res.push_back(el.second);
}
return res;

}

int numof1(int num){
int cnt=0;
while(num){
cnt+=num&1;
num>>=1;
}
return cnt;
}
};

0 comments on commit b176ab2

Please sign in to comment.