191. Number of 1 Bits
·
Algorithm
Number of 1 Bits - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. Bit 성질을 이용한 풀이 class Solution: def hammingWeight(self, n: int) -> int: answer = 0 while n: n = n & (n - 1) answer += 1 return answer 비트 조작은 굉장히 재밌는 성질들이 많다. 내가 아는 몇 안 되는 비트 조작 중 하나가 이것인데, 양의 정수 a와 a-1을 AND 연산하면 양..