leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 085981a47a09f0a12e12ed9b070c15807ea79942
parent af538cc2d8cdfdf28348731cd00125c614080bb9
Author: Andrew Laack <andrew@laack.co>
Date:   Mon, 14 Jul 2025 17:04:32 -0500

Completed number of 1 bits

Diffstat:
Anumber-of-1-bits/number-of-1-bitsV1.py | 15+++++++++++++++
1 file changed, 15 insertions(+), 0 deletions(-)

diff --git a/number-of-1-bits/number-of-1-bitsV1.py b/number-of-1-bits/number-of-1-bitsV1.py @@ -0,0 +1,15 @@ +class Solution: + def hammingWeight(self, n: int) -> int: + return count_bits(n) + + +def count_bits(n: int) -> int: + count = 0 + + while n != 0: + if n % 2 == 1: + count += 1 + n -= 1 + n = n // 2 + + return count