-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0191-number-of-1-bits.js
More file actions
39 lines (35 loc) · 1.07 KB
/
Copy path0191-number-of-1-bits.js
File metadata and controls
39 lines (35 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//Blog: https://www.allenliservice.online/leetcode-js-191-number-of-1-bits/
// <strong>Solution:</strong>
// 1. 宣告 count 次數為 0。
// 2. 宣告 nums 為 n.toString 二進制的字串,且將陣列中的每個數值分割為單一元素。
// <pre style='background-color:#ggg'>
// Ex. Input = 3
// toString(2) = 00000000000000000000000000001011
// toString(2).split("") =
// [
// '0', '0', '0', '0', '0', '0',
// '0', '0', '0', '0', '0', '0',
// '0', '0', '0', '0', '0', '0',
// '0', '0', '0', '0', '0', '0',
// '0', '0', '0', '0', '1', '0',
// '1', '1'
// ]
// </pre>
// 3. 運用「for in」將每個元素皆除以2,如果剩餘1,則 count++。
// 4. 回傳 count 次數。
//<strong>Code 1: BigO(n)</strong>;
var hammingWeight = function (n) {
let count = 0,
nums = n.toString(2).split("");
for (let num in nums) {
if (nums[num] % 2 === 1) {
count++;
}
}
return count;
};
/* <strong>FlowChart:</strong>
<strong>Example 1</strong>
Input: n = 00000000000000000000000000001011
1 23
Output count = 3 */