๐ป Coding Problems Solving/Array | String | Loop
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 2๊ฐ ์ดํ๋ก ๋ค๋ฅธ ๋นํธ
Kim_dev
2022. 4. 18. 14:11
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 2๊ฐ ์ดํ๋ก ๋ค๋ฅธ ๋นํธ
1. ๋ฌธ์ : Link
2๊ฐ ์ดํ์ ๋นํธ๋ฅผ ๋ฐ๊พธ๋ฉด์ ํด๋น ์ซ์๋ณด๋ค ํฐ ์๊ตฌํ๊ธฐ
2. ํ์ด
๊ท์น์ ์ฐพ์๋ด์ ์ ์ฉ
3. ์ฝ๋
def bin_to_dex(n):
return int('0b' + ''.join(n), 2)
def solution(numbers):
answer = []
for num in numbers:
change = False
num = list(bin(num)[2:])
l = len(num)
for i in range(l-1, -1, -1):
if num[i] == '0':
if i == l-1:
num[i] = '1'
answer.append(bin_to_dex(num))
change = True
break
else:
num[i] = '1'
num[i+1] = '0'
answer.append(bin_to_dex(num))
change = True
break
if change == False:
num.insert(0,'1')
num[1] = '0'
answer.append(bin_to_dex(num))
return answer
* bin, dex ๋ณํ ์ ์์๋๊ธฐ
* for๋ฌธ ์ญ์
* join์ ์ด์ ์ธ์ธ๋ ๋์๋..
๋น์ทํ ๋ฌธ์ ,
+1 ํด์ฃผ๋ฉด์ count๋ก 1๊ฐ ๋น๊ต!
def bin_to_dex(n):
return int('0b' + ''.join(n), 2)
def solution(n):
change = False
m = list(bin(n)[2:])
cnt = m.count('1')
while 1:
n += 1
m = list(bin(n)[2:])
cnt2 = m.count('1')
if cnt == cnt2:
return bin_to_dex(m)