https://school.programmers.co.kr/learn/courses/30/lessons/12911
def solution(n):
binary = list(format(n, 'b'))
if '0' not in binary or binary[0] == "1":
binary.insert(0, "0")
swipe_idx = 0
remain_one = -1
for i in range(0, len(binary)):
if i < len(binary)-1 and binary[i] == '0' and binary[i+1] == "1":
swipe_idx = i
remain_one = -1
elif binary[i] == '1':
remain_one += 1
binary[swipe_idx] = '1'
binary[swipe_idx+1] = '0'
for i in range(len(binary)-1, swipe_idx+1, -1):
if remain_one > 0:
binary[i] = "1"
remain_one -= 1
else:
binary[i] = "0"
func solution(_ n:Int) -> Int{
var nOneCount = String(n, radix:2).filter{ $0 == "1" }.count
var value = n+1
while(true){
let radix = String(value, radix:2)
let oneCount = radix.filter{ $0 == "1"}.count
if(oneCount == nOneCount){
break
}
value += 1
}
return value
}
[프로그래머스] H-Index (0) | 2023.12.18 |
---|---|
[프로그래머스] n^2 배열 자르기 (1) | 2023.12.15 |
[프로그래머스] 괄호 회전하기 (0) | 2023.12.15 |
[프로그래머스] 방금 그 곡 (0) | 2023.12.13 |
[프로그래머스] 짝지어 제거하기 (0) | 2023.12.12 |