๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’๊ณต๋ถ€/์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.2] 2๊ฐœ ์ดํ•˜๋กœ ๋‹ค๋ฅธ ๋น„ํŠธ

by rindev 2021. 5. 18.

https://programmers.co.kr/learn/courses/30/lessons/77885

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - 2๊ฐœ ์ดํ•˜๋กœ ๋‹ค๋ฅธ ๋น„ํŠธ

 

programmers.co.kr

 

์ฒ˜์Œ์—๋Š” 2์ง„์ˆ˜๋กœ ๋ฐ”๊ฟ”์„œ 1๊ณผ 0์˜ ๊ฐฏ์ˆ˜๋ฅผ ์„ธ๋Š” ๋ฐฉ๋ฒ•์„ ์‹œ๋„ํ–ˆ์œผ๋‚˜...

์˜ˆ์ œ๋Š” ํ†ต๊ณผํ•˜๋Š”๋ฐ ์ œ์ถœํ–ˆ๋”๋‹ˆ ์ ์ˆ˜๋„ ์—‰๋ง์ด๊ณ  ์‹œ๊ฐ„์ดˆ๊ณผ๋„ ๋‚˜์„œ ๋น„ํŠธ๋กœ ํ‘ธ๋Š” ๋ฒ•์„ ์•Œ์•„๋ดค๋‹คใ… ใ… 

 

 

 

๐Ÿ™‹‍โ™€๏ธ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

์ฝ”๋“œ ๋‚ด์˜ ์ฃผ์„์€ ์ง€์šด ์ƒํƒœ! ์œ„์— ์„ค๋ช…์ด ์žˆ์œผ๋‹ˆ๊นŒ์šฉ...

๋”๋ณด๊ธฐ
def solution(numbers):
    answer = []
    
    def cal(n):
        last = (~n) & (n+1) #์ตœํ•˜์œ„๋น„ํŠธ 0์ด ์žˆ๋Š” ์œ„์น˜
        return (n|last) & ~(last>>1)        
    
    for n in numbers:
        if n%2 == 0 :
            answer.append(n+1)
        else: #ํ™€์ˆ˜์ธ๊ฒฝ์šฐ
            tmp = cal(n)
            answer.append(tmp)
    
    return answer

 

 

๋Œ“๊ธ€