Skip to content

Commit 0439ac7

Browse files
committed
sadie100: number of 1 bits solution
1 parent 1f732f5 commit 0439ac7

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

number-of-1-bits/sadie100.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
n을 이진수 변환하고 1의 개수를 센다
3+
4+
시간복잡도 O(N) (N은 숫자 n의 비트 개수)
5+
*/
6+
7+
function hammingWeight(n: number): number {
8+
const binaryNum = n.toString(2)
9+
let result = 0
10+
for (let char of binaryNum) {
11+
if (char === '1') result += 1
12+
}
13+
return result
14+
}

0 commit comments

Comments
 (0)