leetcode

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

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

Completed number of 1 bits

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

diff --git a/number-of-1-bits/number-of-1-bitsV2.py b/number-of-1-bits/number-of-1-bitsV2.py @@ -0,0 +1,14 @@ +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 & 1 == 1: + count += 1 + n = n >> 1 + + return count