Skip to content

Commit 5ba5fee

Browse files
committed
Add solution #191
1 parent 8290139 commit 5ba5fee

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
151|[Reverse Words in a String](./0151-reverse-words-in-a-string.js)|Medium|
5757
152|[Maximum Product Subarray](./0152-maximum-product-subarray.js)|Medium|
5858
179|[Largest Number](./0179-largest-number.js)|Medium|
59+
191|[Number of 1 Bits](./0191-number-of-1-bits.js)|Easy|
5960
203|[Remove Linked List Elements](./0203-remove-linked-list-elements.js)|Easy|
6061
206|[Reverse Linked List](./0206-reverse-linked-list.js)|Easy|
6162
217|[Contains Duplicate](./0217-contains-duplicate.js)|Easy|

solutions/0191-number-of-1-bits.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* 191. Number of 1 Bits
3+
* https://leetcode.com/problems/number-of-1-bits/
4+
* Difficulty: Easy
5+
*
6+
* Write a function that takes an unsigned integer and returns the number of
7+
* '1' bits it has (also known as the Hamming weight).
8+
*/
9+
10+
/**
11+
* @param {number} n - a positive integer
12+
* @return {number}
13+
*/
14+
var hammingWeight = function(n) {
15+
return n.toString(2).match(/1/g)?.length || 0;
16+
};

0 commit comments

Comments
 (0)