Skip to content

Commit 7e16408

Browse files
iamAntimPalAntim-IWPIamShiwangi
committed
Create readme.md
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com>
1 parent c1a902d commit 7e16408

File tree

1 file changed

+221
-0
lines changed

1 file changed

+221
-0
lines changed
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
---
2+
comments: true
3+
difficulty: Easy
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0300-0399/0338.Counting%20Bits/README_EN.md
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+
[中文文档](/solution/0300-0399/0338.Counting%20Bits/README.md)
15+
16+
## Description
17+
18+
<!-- description:start -->
19+
20+
<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 &lt;= i &lt;= n</code>)<em>, </em><code>ans[i]</code><em> is the <strong>number of </strong></em><code>1</code><em><strong>&#39;s</strong> in the binary representation of </em><code>i</code>.</p>
21+
22+
<p>&nbsp;</p>
23+
<p><strong class="example">Example 1:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> n = 2
27+
<strong>Output:</strong> [0,1,1]
28+
<strong>Explanation:</strong>
29+
0 --&gt; 0
30+
1 --&gt; 1
31+
2 --&gt; 10
32+
</pre>
33+
34+
<p><strong class="example">Example 2:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> n = 5
38+
<strong>Output:</strong> [0,1,1,2,1,2]
39+
<strong>Explanation:</strong>
40+
0 --&gt; 0
41+
1 --&gt; 1
42+
2 --&gt; 10
43+
3 --&gt; 11
44+
4 --&gt; 100
45+
5 --&gt; 101
46+
</pre>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>0 &lt;= n &lt;= 10<sup>5</sup></code></li>
53+
</ul>
54+
55+
<p>&nbsp;</p>
56+
<p><strong>Follow up:</strong></p>
57+
58+
<ul>
59+
<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>
60+
<li>Can you do it without using any built-in function (i.e., like <code>__builtin_popcount</code> in C++)?</li>
61+
</ul>
62+
63+
<!-- description:end -->
64+
65+
## Solutions
66+
67+
<!-- solution:start -->
68+
69+
### Solution 1
70+
71+
<!-- tabs:start -->
72+
73+
#### Python3
74+
75+
```python
76+
class Solution:
77+
def countBits(self, n: int) -> List[int]:
78+
return [i.bit_count() for i in range(n + 1)]
79+
```
80+
81+
#### Java
82+
83+
```java
84+
class Solution {
85+
public int[] countBits(int n) {
86+
int[] ans = new int[n + 1];
87+
for (int i = 0; i <= n; ++i) {
88+
ans[i] = Integer.bitCount(i);
89+
}
90+
return ans;
91+
}
92+
}
93+
```
94+
95+
#### C++
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
vector<int> countBits(int n) {
101+
vector<int> ans(n + 1);
102+
for (int i = 0; i <= n; ++i) {
103+
ans[i] = __builtin_popcount(i);
104+
}
105+
return ans;
106+
}
107+
};
108+
```
109+
110+
#### Go
111+
112+
```go
113+
func countBits(n int) []int {
114+
ans := make([]int, n+1)
115+
for i := 0; i <= n; i++ {
116+
ans[i] = bits.OnesCount(uint(i))
117+
}
118+
return ans
119+
}
120+
```
121+
122+
#### TypeScript
123+
124+
```ts
125+
function countBits(n: number): number[] {
126+
const ans: number[] = Array(n + 1).fill(0);
127+
for (let i = 0; i <= n; ++i) {
128+
ans[i] = bitCount(i);
129+
}
130+
return ans;
131+
}
132+
133+
function bitCount(n: number): number {
134+
let count = 0;
135+
while (n) {
136+
n &= n - 1;
137+
++count;
138+
}
139+
return count;
140+
}
141+
```
142+
143+
<!-- tabs:end -->
144+
145+
<!-- solution:end -->
146+
147+
<!-- solution:start -->
148+
149+
### Solution 2
150+
151+
<!-- tabs:start -->
152+
153+
#### Python3
154+
155+
```python
156+
class Solution:
157+
def countBits(self, n: int) -> List[int]:
158+
ans = [0] * (n + 1)
159+
for i in range(1, n + 1):
160+
ans[i] = ans[i & (i - 1)] + 1
161+
return ans
162+
```
163+
164+
#### Java
165+
166+
```java
167+
class Solution {
168+
public int[] countBits(int n) {
169+
int[] ans = new int[n + 1];
170+
for (int i = 1; i <= n; ++i) {
171+
ans[i] = ans[i & (i - 1)] + 1;
172+
}
173+
return ans;
174+
}
175+
}
176+
```
177+
178+
#### C++
179+
180+
```cpp
181+
class Solution {
182+
public:
183+
vector<int> countBits(int n) {
184+
vector<int> ans(n + 1);
185+
for (int i = 1; i <= n; ++i) {
186+
ans[i] = ans[i & (i - 1)] + 1;
187+
}
188+
return ans;
189+
}
190+
};
191+
```
192+
193+
#### Go
194+
195+
```go
196+
func countBits(n int) []int {
197+
ans := make([]int, n+1)
198+
for i := 1; i <= n; i++ {
199+
ans[i] = ans[i&(i-1)] + 1
200+
}
201+
return ans
202+
}
203+
```
204+
205+
#### TypeScript
206+
207+
```ts
208+
function countBits(n: number): number[] {
209+
const ans: number[] = Array(n + 1).fill(0);
210+
for (let i = 1; i <= n; ++i) {
211+
ans[i] = ans[i & (i - 1)] + 1;
212+
}
213+
return ans;
214+
}
215+
```
216+
217+
<!-- tabs:end -->
218+
219+
<!-- solution:end -->
220+
221+
<!-- problem:end -->

0 commit comments

Comments
 (0)