LeetCode problem#338
Counting Bits
Solved in 1 languageJava
Solution
1class Solution {
2 public int[] countBits(int n) {
3 int[] counts = new int[n + 1];
4 counts[0] = 0;
5
6 for (int i = 1; i < counts.length; i++) {
7 if ((i & 1) == 1) { // odd
8 // an odd number ends in 1 binary.
9 // if you subtract one, it ends in 0 binary.
10 // so you take the count of the previous plus one,
11 counts[i] = counts[i - 1] + 1;
12 }
13 else {
14 // an even number ends in 0 binary.
15 // the count of ones is the same as the number
16 // with bits shifted to the right.
17 // e.g. 0110 -> 0011
18 // (6) -> (3)
19 counts[i] = counts[i >> 1];
20 }
21 }
22
23 return counts;
24 }
25}How it works
The provided solution is for the problem of counting the number of 1s (bits that are set) in the binary representation of all numbers from 0 to n. The method employed utilizes dynamic programming to efficiently compute the results. Let's break down the implementation step by step:
Class and Method Definition
class Solution {
public int[] countBits(int n) {- We define a class named
Solution, and within it, a public methodcountBitsthat accepts an integernwhich is the upper limit (inclusive) for which we want to count the bits.
Initialize the Array
int[] counts = new int[n + 1];
counts[0] = 0;- We create an integer array
countsof sizen + 1to store the count of 1s for each number from0ton. - We initialize
counts[0]to0because the binary representation of0has zero bits set.
Fill the Counts Array
for (int i = 1; i < counts.length; i++) {-
We iterate through all numbers from
1ton. -
Within each iteration, we determine if the current number
iis odd or even:
if ((i & 1) == 1) { // oddCase for Odd Numbers
- If
iis odd (i.e., the least significant bit is1), we can observe that:- An odd number in binary ends with
1. For example1 (01),3 (11),5 (101), etc. - If we subtract
1from it, it becomes an even number (which ends with0).
- An odd number in binary ends with
Thus:
counts[i] = counts[i - 1] + 1;- We can derive the count of
1sfor the odd numberifrom the count of1sof the previous numberi - 1(which has the same number of1sasiexcept it loses the1from the LSB), then add1to it (for the1bit thatihas at the end).
Case for Even Numbers
} else { // even
counts[i] = counts[i >> 1];- If
iis even (with least significant bit0), its binary representation can be thought to be like shifting right to remove the last0. - We can find the count of
1sof this number by looking at the number derived by right shiftingi, which isi >> 1. - For instance,
6 (0110)shifts to3 (0011), and thus the count of1sis the same as the count of1sin3.
Return the Result
}
return counts;
}- Finally, we return the
countsarray, which now contains the number of1sfor each number from0ton.
Complexity
- Time Complexity: (O(n)), since we only loop through numbers from
1tononce. - Space Complexity: (O(n)), due to the usage of an additional array to store the counts.
Conclusion
This solution smartly leverages properties of binary numbers, particularly focusing on the odd/even nature of numbers and their relationships, effectively resulting in a very efficient way to count the bits.
Written by AI from the code above, so double-check anything important.
Problem statements belong to LeetCode and are on leetcode.com. We're not affiliated with LeetCode.