Categorygithub.com/szhou12/leetcode-goleetcode1356-Sort-Integers-by-The-Number-of-1-Bits
package
0.0.0-20241220224003-b7cf03a90b2b
Repository: https://github.com/szhou12/leetcode-go.git
Documentation: pkg.go.dev

# README

1356. Sort Integers by The Number of 1 Bits

Solution idea

Naive solution

  • convert a number to its binary representation
  • sort by number of 1's

Bit Operation

  • 循环n的二进制表示中1的个数次
int bitCount(int n) {
    int count = 0;
    while (n) {
        n &= (n - 1); // 清除最低位的1
        count++;
    }
    return count;
}

Resource

代码随想录-1356. 根据数字二进制下 1 的数目排序