Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 9. 23. 23:45

https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/

 

Concatenation of Consecutive Binary Numbers - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

주어진 조건으로 그냥 풀면 TLE가 남.. 그래서 뒷자리 몇개를 잘라 봤는데 값이 이상하게 나옴.
결국 또 솔루션 확인 ㅠ_ㅠ

''' 
# WRONG ANSWER
class Solution:
    def concatenatedBinary(self, n: int) -> int:
        b = ''
        for i in range(n, 0, -1):
            b = bin(i).replace('0b', '') + b    
        return int('0b' + b, 2) % (10**9 + 7)
'''

이상하네.. 정방향으로 하면 문제 없이 풀림 =_=
뭐 땜시 그러는거지..

class Solution:
    def concatenatedBinary(self, n: int) -> int:
        b = ''
        for i in range(1, n+1):
            b += format(i, 'b')
        return int(b, 2) % (10**9 + 7)
반응형