Skip to content

Commit 6651f77

Browse files
committed
feat: add solutions to lc problem: No.0191
1 parent 4192b92 commit 6651f77

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
lines changed

solution/0100-0199/0191.Number of 1 Bits/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,22 @@ int hammingWeight(uint32_t n) {
206206
}
207207
```
208208
209+
#### Kotlin
210+
211+
```kotlin
212+
class Solution {
213+
fun hammingWeight(n: Int): Int {
214+
var count = 0
215+
var num = n
216+
while (num != 0) {
217+
num = num and (num - 1)
218+
count++
219+
}
220+
return count
221+
}
222+
}
223+
```
224+
209225
<!-- tabs:end -->
210226

211227
<!-- solution:end -->
@@ -277,6 +293,19 @@ func hammingWeight(num uint32) int {
277293
}
278294
```
279295

296+
#### TypeScript
297+
298+
```ts
299+
function hammingWeight(n: number): number {
300+
let count = 0;
301+
while (n) {
302+
n -= n & -n;
303+
count++;
304+
}
305+
return count;
306+
}
307+
```
308+
280309
#### Rust
281310

282311
```rust
@@ -292,6 +321,39 @@ impl Solution {
292321
}
293322
```
294323

324+
#### JavaScript
325+
326+
```js
327+
/**
328+
* @param {number} n
329+
* @return {number}
330+
*/
331+
var hammingWeight = function (n) {
332+
let count = 0;
333+
while (n) {
334+
n -= n & -n;
335+
count++;
336+
}
337+
return count;
338+
};
339+
```
340+
341+
#### Kotlin
342+
343+
```kotlin
344+
class Solution {
345+
fun hammingWeight(n: Int): Int {
346+
var count = 0
347+
var num = n
348+
while (num != 0) {
349+
num -= num and (-num)
350+
count++
351+
}
352+
return count
353+
}
354+
}
355+
```
356+
295357
<!-- tabs:end -->
296358

297359
<!-- solution:end -->

solution/0100-0199/0191.Number of 1 Bits/README_EN.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,22 @@ int hammingWeight(uint32_t n) {
186186
}
187187
```
188188
189+
#### Kotlin
190+
191+
```kotlin
192+
class Solution {
193+
fun hammingWeight(n: Int): Int {
194+
var count = 0
195+
var num = n
196+
while (num != 0) {
197+
num = num and (num - 1)
198+
count++
199+
}
200+
return count
201+
}
202+
}
203+
```
204+
189205
<!-- tabs:end -->
190206

191207
<!-- solution:end -->
@@ -253,6 +269,19 @@ func hammingWeight(num uint32) int {
253269
}
254270
```
255271

272+
#### TypeScript
273+
274+
```ts
275+
function hammingWeight(n: number): number {
276+
let count = 0;
277+
while (n) {
278+
n -= n & -n;
279+
count++;
280+
}
281+
return count;
282+
}
283+
```
284+
256285
#### Rust
257286

258287
```rust
@@ -268,6 +297,39 @@ impl Solution {
268297
}
269298
```
270299

300+
#### JavaScript
301+
302+
```js
303+
/**
304+
* @param {number} n
305+
* @return {number}
306+
*/
307+
var hammingWeight = function (n) {
308+
let count = 0;
309+
while (n) {
310+
n -= n & -n;
311+
count++;
312+
}
313+
return count;
314+
};
315+
```
316+
317+
#### Kotlin
318+
319+
```kotlin
320+
class Solution {
321+
fun hammingWeight(n: Int): Int {
322+
var count = 0
323+
var num = n
324+
while (num != 0) {
325+
num -= num and (-num)
326+
count++
327+
}
328+
return count
329+
}
330+
}
331+
```
332+
271333
<!-- tabs:end -->
272334

273335
<!-- solution:end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
fun hammingWeight(n: Int): Int {
3+
var count = 0
4+
var num = n
5+
while (num != 0) {
6+
num = num and (num - 1)
7+
count++
8+
}
9+
return count
10+
}
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var hammingWeight = function (n) {
6+
let count = 0;
7+
while (n) {
8+
n -= n & -n;
9+
count++;
10+
}
11+
return count;
12+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
fun hammingWeight(n: Int): Int {
3+
var count = 0
4+
var num = n
5+
while (num != 0) {
6+
num -= num and (-num)
7+
count++
8+
}
9+
return count
10+
}
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function hammingWeight(n: number): number {
2+
let count = 0;
3+
while (n) {
4+
n -= n & -n;
5+
count++;
6+
}
7+
return count;
8+
}

0 commit comments

Comments
 (0)