一、题目
二、代码
class Solution
{
public:
//排序算法中加入谓词
static int cal_binary_bit(int process_num)
{
int return_count=0;
while(process_num!=0)
{
if(process_num%2==1) return_count++;
process_num=process_num/2;
}
return return_count;
}
static bool compare(int num1, int num2)
{
int bit_of_num1=cal_binary_bit(num1);
int bit_of_num2=cal_binary_bit(num2);
if(bit_of_num1==bit_of_num2) return num1<num2;
return bit_of_num1<bit_of_num2;
}
vector<int> sortByBits(vector<int>& arr)
{
sort(arr.begin(),arr.end(),compare);
return arr;
}
};