|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Easy |
| 4 | +edit_url: |
| 5 | +tags: |
| 6 | + - Bit Manipulation |
| 7 | + - Dynamic Programming |
| 8 | +--- |
| 9 | + |
| 10 | +<!-- problem:start --> |
| 11 | + |
| 12 | +# [338. Counting Bits](https://leetcode.com/problems/counting-bits) |
| 13 | + |
| 14 | + |
| 15 | +## Description |
| 16 | + |
| 17 | +<!-- description:start --> |
| 18 | + |
| 19 | +<p>Given an integer <code>n</code>, return <em>an array </em><code>ans</code><em> of length </em><code>n + 1</code><em> such that for each </em><code>i</code><em> </em>(<code>0 <= i <= n</code>)<em>, </em><code>ans[i]</code><em> is the <strong>number of </strong></em><code>1</code><em><strong>'s</strong> in the binary representation of </em><code>i</code>.</p> |
| 20 | + |
| 21 | +<p> </p> |
| 22 | +<p><strong class="example">Example 1:</strong></p> |
| 23 | + |
| 24 | +<pre> |
| 25 | +<strong>Input:</strong> n = 2 |
| 26 | +<strong>Output:</strong> [0,1,1] |
| 27 | +<strong>Explanation:</strong> |
| 28 | +0 --> 0 |
| 29 | +1 --> 1 |
| 30 | +2 --> 10 |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><strong class="example">Example 2:</strong></p> |
| 34 | + |
| 35 | +<pre> |
| 36 | +<strong>Input:</strong> n = 5 |
| 37 | +<strong>Output:</strong> [0,1,1,2,1,2] |
| 38 | +<strong>Explanation:</strong> |
| 39 | +0 --> 0 |
| 40 | +1 --> 1 |
| 41 | +2 --> 10 |
| 42 | +3 --> 11 |
| 43 | +4 --> 100 |
| 44 | +5 --> 101 |
| 45 | +</pre> |
| 46 | + |
| 47 | +<p> </p> |
| 48 | +<p><strong>Constraints:</strong></p> |
| 49 | + |
| 50 | +<ul> |
| 51 | + <li><code>0 <= n <= 10<sup>5</sup></code></li> |
| 52 | +</ul> |
| 53 | + |
| 54 | +<p> </p> |
| 55 | +<p><strong>Follow up:</strong></p> |
| 56 | + |
| 57 | +<ul> |
| 58 | + <li>It is very easy to come up with a solution with a runtime of <code>O(n log n)</code>. Can you do it in linear time <code>O(n)</code> and possibly in a single pass?</li> |
| 59 | + <li>Can you do it without using any built-in function (i.e., like <code>__builtin_popcount</code> in C++)?</li> |
| 60 | +</ul> |
| 61 | + |
| 62 | +<!-- description:end --> |
| 63 | + |
| 64 | +## Solutions |
| 65 | + |
| 66 | +<!-- solution:start --> |
| 67 | + |
| 68 | +### Solution 1 |
| 69 | + |
| 70 | +<!-- tabs:start --> |
| 71 | + |
| 72 | +#### Python3 |
| 73 | + |
| 74 | +```python |
| 75 | +class Solution: |
| 76 | + def countBits(self, n: int) -> List[int]: |
| 77 | + return [i.bit_count() for i in range(n + 1)] |
| 78 | +``` |
| 79 | + |
| 80 | +#### Java |
| 81 | + |
| 82 | +```java |
| 83 | +class Solution { |
| 84 | + public int[] countBits(int n) { |
| 85 | + int[] ans = new int[n + 1]; |
| 86 | + for (int i = 0; i <= n; ++i) { |
| 87 | + ans[i] = Integer.bitCount(i); |
| 88 | + } |
| 89 | + return ans; |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +#### C++ |
| 95 | + |
| 96 | +```cpp |
| 97 | +class Solution { |
| 98 | +public: |
| 99 | + vector<int> countBits(int n) { |
| 100 | + vector<int> ans(n + 1); |
| 101 | + for (int i = 0; i <= n; ++i) { |
| 102 | + ans[i] = __builtin_popcount(i); |
| 103 | + } |
| 104 | + return ans; |
| 105 | + } |
| 106 | +}; |
| 107 | +``` |
| 108 | +
|
| 109 | +
|
| 110 | +<!-- tabs:end --> |
| 111 | +
|
| 112 | +<!-- solution:end --> |
| 113 | +
|
| 114 | +<!-- problem:end --> |
0 commit comments