LeetCode 1356 | Sort Integers by Number of 1 Bits | Brian Kernighan | Java | Easy
Автор: DRACO
Загружено: 2026-02-25
Просмотров: 17
Описание:
LeetCode 1356 – Sort Integers by The Number of 1 Bits (Java)
Today's LeetCode Daily
Given an integer array arr,
sort the numbers by:
1. Number of set bits in binary representation
2. If tie, sort by numeric value
Approach Used:
Step 1:
Count set bits using Brian Kernighan’s Algorithm.
Logic of Brian Kernighan:
Repeatedly do:
n = n and (n minus 1)
Each operation removes the lowest set bit.
Count how many times it runs.
Step 2:
Store each number along with its bit count.
Step 3:
Sort using custom comparator:
First by bit count
Then by actual number
Step 4:
Extract sorted numbers into result array.
Key Idea:
Efficient bit counting plus custom sorting.
Time Complexity: O(n log n)
Space Complexity: O(n)
Problem Link:
https://leetcode.com/problems/sort-in...
Java Solution (GitHub):
https://github.com/Amandf/LeetCode-So...
More daily Java solutions.
Clean code. No mic. Pure logic.
#leetcode #leetcodedailychallenge #java #bitmanipulation #sorting #dsa #codinginterview #programming
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: