You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 20, 2024. It is now read-only.
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.
4
+
5
+
6
+
## Example 1:
7
+
8
+
Input: n = 2
9
+
Output: [0,1,1]
10
+
Explanation:
11
+
0 --> 0
12
+
1 --> 1
13
+
2 --> 10
14
+
15
+
## Example 2:
16
+
17
+
Input: n = 5
18
+
Output: [0,1,1,2,1,2]
19
+
Explanation:
20
+
0 --> 0
21
+
1 --> 1
22
+
2 --> 10
23
+
3 --> 11
24
+
4 --> 100
25
+
5 --> 101
26
+
27
+
28
+
## Constraints:
29
+
30
+
0 <= n <= 105
31
+
32
+
33
+
## Follow up:
34
+
35
+
It is very easy to come up with a solution with a runtime of O(n log n). Can you do it in linear time O(n) and possibly in a single pass?
36
+
Can you do it without using any built-in function (i.e., like __builtin_popcount in C++)?
0 commit comments