Leetcode Problem 1356. Sort Integers by The Number of 1 Bits

1356. Sort Integers by The Number of 1 Bits

Leetcode Solutions

Sort By Custom Comparator: Bit Manipulation

  1. Define a function hammingWeight that calculates the Hamming weight of a number using Brian Kernighan's algorithm.
  2. Define a custom comparator that uses the Hamming weight to compare two numbers. If the Hamming weights are equal, compare the numbers themselves.
  3. Sort the array arr using the custom comparator.
  4. Return the sorted array.
UML Thumbnail

Sort By Counting Bits and Using Auxiliary Array

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...