class Solution {
public:
struct cmp{
bool operator()(const int &x,const int &y)
{
if(__builtin_popcount(x)>__builtin_popcount(y))
{
return true;
}
if(__builtin_popcount(x)==__builtin_popcount(y))
{
if(x>y)
{
return true;
}
else
{
return false;
}
}
return false;
}
};
vector<int> sortByBits(vector<int>& arr) {
//用_builtin_popcount()函数计算每一个数字的二进制形式中1的个数
sort(arr.begin(),arr.end(),cmp());
reverse(arr.begin(),arr.end());
return arr;
}
};
LeetCode:1356. 根据数字二进制下 1 的数目排序
最新推荐文章于 2020-11-07 16:52:16 发布