@@ -107,112 +107,6 @@ public:
107107};
108108```
109109
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- ```
216110
217111<!-- tabs:end -->
218112
0 commit comments