Description
Given an integer n
, return an array ans
of length n + 1
such that for each i
(0 <= i <= n
), ans[i]
is the number of 1
‘s in the binary representation of i
.
Examples:
Example 1:
Input: n = 2
Output: [0,1,1]
Explanation:
0 --> 0
1 --> 1
2 --> 10
Example 2:
Input: n = 5
Output: [0,1,1,2,1,2]
Explanation:
0 --> 0
1 --> 1
2 --> 10
3 --> 11
4 --> 100
5 --> 101
Solution in Python
Plan:
- Initialize an array
ans
of size n+1 with all elements set to 0. - Iterate through numbers from 1 to n, and for each number i:
- If i is even, ans[i]=ans[i>>1].
- If i is odd, ans[i]=ans[i>>1]+1.
- Return the
ans
array.
Python
class Solution:
def countBits(self, n: int) -> List[int]:
# Initialize an array of size n + 1 to store the number of 1's for each i
ans = [0] * (n + 1)
# Iterate from 1 to n to compute the number of 1's in the binary representation
for i in range(1, n + 1):
# ans[i >> 1] gives the result for i // 2, we add 1 if i is odd
ans[i] = ans[i >> 1] + (i & 1) # i & 1 checks if the last bit is 1 (odd number)
return ans
Detailed Explanation:
- Line 1: We import the
List
type from thetyping
module for type hinting. - Line 4: The function
countBits
is defined, which takes an integern
as input and returns a list of integers. - Line 6: We initialize a list
ans
of size n+1 filled with zeros. This list will store the number of 1’s in the binary representation of each number from 0 to n. - Lines 9-11: We iterate over all integers from 1 to n:
- For each integer iii, we calculate the number of 1’s in its binary representation by reusing the result of i>>1 (which is equivalent to integer division by 2) and adding 1 if i is odd (
i & 1
checks whether the last bit of i is 1).
- For each integer iii, we calculate the number of 1’s in its binary representation by reusing the result of i>>1 (which is equivalent to integer division by 2) and adding 1 if i is odd (
- Line 13: Finally, the function returns the list
ans
, which contains the number of 1’s in the binary representation for each number from 0 to n.
Solution in C++
C++
class Solution {
public:
vector<int> countBits(int n) {
// Initialize a vector 'ans' of size n + 1 to store the result
vector<int> ans(n + 1, 0);
// Loop over each number from 1 to n
for (int i = 1; i <= n; ++i) {
// The number of 1's in i is the number of 1's in i/2 (i >> 1) plus i % 2 (i & 1)
ans[i] = ans[i >> 1] + (i & 1);
}
// Return the result array
return ans;
}
};
Solution in Javascript
JavaScript
var countBits = function(n) {
// Create an array 'ans' of length n + 1 to store the result.
// Initially, all elements are set to 0.
const ans = new Array(n + 1).fill(0);
// Iterate from 1 to n, filling in the 'ans' array.
for (let i = 1; i <= n; i++) {
// The number of 1's in the binary representation of 'i' can be derived
// using the value for 'i >> 1' (which is 'i' divided by 2).
// We add 1 to this value if 'i' is odd (i.e., 'i & 1' equals 1).
ans[i] = ans[i >> 1] + (i & 1);
}
// Return the resulting array 'ans' that contains the number of 1's in the binary representation for each number.
return ans;
};
Solution in Java
Java
class Solution {
// Function to count the number of 1's in binary representation of numbers from 0 to n
public int[] countBits(int n) {
// Array to store the result (number of 1's for each number from 0 to n)
int[] ans = new int[n + 1];
// Start from 0 up to n
for (int i = 1; i <= n; i++) {
// If i is even, the number of 1's is the same as the number of 1's in i / 2
// If i is odd, it's the same as i / 2 but with an additional 1 (because of the least significant bit)
ans[i] = ans[i >> 1] + (i & 1);
}
// Return the array containing the result for each number
return ans;
}
}