We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0adea9c commit df1514aCopy full SHA for df1514a
number-of-1-bits/naringst.js
@@ -0,0 +1,29 @@
1
+/**
2
+ * @param {number} n
3
+ * @return {number}
4
+ */
5
+
6
7
+ * Runtime: 59ms, Memory: 50.76MB
8
+ *
9
+ * Time complexity: O(logN)
10
+ * -> While
11
+ * Space complexity: O(logN)
12
+ * -> To save binary.split()
13
14
+ * **/
15
16
+var hammingWeight = function (n) {
17
+ let binary = "";
18
19
+ while (n > 1) {
20
+ let left = n % 2;
21
+ binary += left.toString();
22
+ n = Math.floor(n / 2);
23
+ }
24
+ binary = binary + n.toString();
25
26
+ const count = binary.split("1").length - 1;
27
28
+ return count;
29
+};
0 commit comments