本文记录的是刷题过程中的重要概念和笔记。如有侵权,请联系删除。
1356. 根据数字二进制下 1 的数目排序
力扣题目链接(opens new window)
题目链接:https://leetcode.cn/problems/sort-integers-by-the-number-of-1-bits/
给你一个整数数组 arr 。请你将数组中的元素按照其二进制表示中数字 1 的数目升序排序。
如果存在多个数字二进制中 1 的数目相同,则必须将它们按照数值大小升序排列。
请你返回排序后的数组。
示例 1:
输入:arr = [0,1,2,3,4,5,6,7,8]
输出:[0,1,2,4,8,3,5,6,7]
解释:[0] 是唯一一个有 0 个 1 的数。 [1,2,4,8] 都有 1 个 1 。 [3,5,6] 有 2 个 1 。 [7] 有 3 个 1 。按照 1 的个数排序得到的结果数组为 [0,1,2,4,8,3,5,6,7]
示例 2:
输入:arr = [1024,512,256,128,64,32,16,8,4,2,1]
输出:[1,2,4,8,16,32,64,128,256,512,1024]
解释:数组中所有整数二进制下都只有 1 个 1 ,所以你需要按照数值大小将它们排序。
示例 3:
输入:arr = [10000,10000]
输出:[10000,10000]
示例 4:
输入:arr = [2,3,5,7,11,13,17,19]
输出:[2,3,5,17,7,11,13,19]
示例 5:
输入:arr = [10,100,1000,10000]
输出:[10,100,10000,1000]
思路
如何计算一个数的二进制中1的数量。
我提供两种方法:
方法一
朴实无华挨个计算1的数量,最多就是循环n的二进制位数,32位。
int bitCount(int n) {
int count = 0; // 计数器
while (n > 0) {
if((n & 1) == 1) count++; // 当前位是1,count++
n >>= 1 ; // n向右移位
}
return count;
}
方法二(推荐)
这种方法,只循环n的二进制中1的个数次,比方法一高效的多
int bitCount(int n) {
int count = 0;
while (n) {
n &= (n - 1); // 清除最低位的1
count++;
}
return count;
}
具体实现很巧妙,但是也存在疑问
class Solution {
public:
static int bitCount(int n){ //static???
int count=0;
while(n){
n&=(n-1);
count++;
}
return count;
}
static bool cmp(int a,int b){ //static???
int bitA=bitCount(a);
int bitB=bitCount(b);
if(bitA==bitB) return a<b;
return bitA<bitB; //???
}
vector<int> sortByBits(vector<int>& arr) {
sort(arr.begin(),arr.end(),cmp);
return arr;
}
};