어려운 풀이는 아니었지만 시간초과가 났었다...
format(len(s) - remove0, "b") 라고 코드를 작성했을 때 시간초과가 났었는데,
format(len(s.replace("0", "")), "b") 라고 수정하니 시간초과가 나지 않았다...
문자열 제거에는 replace를 잘 활용해야겠다
소스 코드
def solution(s):
answer = []
count = 0
remove0 = 0
while True:
if s == '1':
break
else:
remove0 += s.count('0')
s = format(len(s.replace("0","")), "b")
count += 1
answer.append(count)
answer.append(remove0)
return answer
def solution(s):
a, b = 0, 0
while s != '1':
a += 1
num = s.count('1')
b += len(s) - num
s = bin(num)[2:]
return [a, b]