n을 2진법으로 변환했을 때 1의 개수를 구하고
n을 초과하는 제일 작은 수의 2진법 수의 1의 개수가 2진법으로 변환한 n의 1의 개수와 동일할 때 반환한다.
def solution(n):
one_count_n = count_one(bin(n)[2:])
m = n + 1
while True:
if one_count_n == count_one(bin(m)[2:]):
return m
m += 1
def count_one(n):
return sum(1 for c in str(n) if c == '1')
str1.count(str2) 로 str1 에 포함된 str2 의 개수를 구할 수 있다.
2진법에 포함된 1의 개수를 찾을 때, [2:]는 불필요하다. 왜냐하면, "0b" 에는 1이 없기 때문이다.
# asis
count_one(bin(m)[2:])
# tobe
bin(m).count('1')