728x90
나의 풀이
def solution(n):
answer = 0
while n > 0:
if n % 2:
n -= 1
answer += 1
else:
n //= 2
return answer
이진법을 활용한 풀이
def solution(n):
return bin(n).count('1')
반응형
'Coding Test > Programmers' 카테고리의 다른 글
[프로그래머스/파이썬] n^2 배열 자르기 (0) | 2022.06.17 |
---|---|
[프로그래머스/파이썬] 3 x n 타일링 (0) | 2022.06.17 |
[프로그래머스/파이썬] 이진 변환 반복하기 (0) | 2022.06.17 |
[프로그래머스/파이썬] [1차] 캐시 (0) | 2022.06.17 |
[프로그래머스/파이썬] 스킬트리 (0) | 2022.06.17 |