简单题
class Solution {
public:
int func(int num){
int count = 0;
while(num!=0){
if (num%2==1){
count ++;
}
num = num/2;
}
return count;
}
vector<int> sortByBits(vector<int>& arr) {
// sort nums
sort(arr.begin(), arr.end());
// gain the 1 nums
map<int, vector<int>> cnt;
for(int i=0;i<arr.size();i++){
int val = func(arr[i]);
if (cnt.find(val)==cnt.end()){
vector<int> temp(1, arr[i]);
cnt[val] = temp;
}
else{
cnt[val].push_back(arr[i]);
}
}
// cout val
vector<int> res;
for(auto it = cnt.begin();it!=cnt.end();it++){
vector<int> temp = it->second;
for(int i=0;i<temp.size();i++){
res.push_back(temp[i]);
}
}
return res;
}
};