프로그래머스_다음 큰 숫자

임정민·2023년 5월 24일
1

알고리즘 문제풀이

목록 보기
50/173
post-thumbnail

프로그래머스 Python 문제풀이 입니다.

문제

https://school.programmers.co.kr/learn/courses/30/lessons/12911

[나의 풀이]

def solution(n):
    
    for i in range(n+1,1000001):
        if str(bin(i)[2:].count('1')) == str(bin(n)[2:].count('1')):
            answer = i
            break;
    return answer

정수를 이진변환시키는 bin()와 문자열 내의 특정 문자의 갯수를 반환하는 string.count()함수로 쉽게 풀 수 있었습니다.🐱🐱🐱

저의 풀이 이외에 다음과 같이

[다른사람의 풀이]

def nextBigNumber(n, count = 0):
    return n if bin(n).count("1") is count else nextBigNumber(n+1, bin(n).count("1") if count is 0 else count)

one line if문과 재귀함수로 구현한 풀이도 볼 수 있었습니다,🐏🐏🐏

감사합니다.

profile
https://github.com/min731

0개의 댓글