반응형
    
    
    
  
														Notice
														
												
											
												
												
													Recent Posts
													
											
												
												
													Recent Comments
													
											
												
												- Today
- Total
작심삼일
[LeetCode] 1680 | Concatenation of Consecutive Binary Numbers | Python 본문
			스터디/코테
			
		[LeetCode] 1680 | Concatenation of Consecutive Binary Numbers | Python
yun_s 2022. 9. 23. 09:59728x90
    
    
  반응형
    
    
    
  문제 링크: https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/
문제
Given an integer n, return the decimal value of the binary string formed by concatenating the binary representations of 1 to n in order, modulo $10^9+7$.
조건
- 1 <= n <= $10^5$
내 풀이
문제에서 시키는대로 n까지의 숫자를 bin()함수를 이용해 이진수로 바꾼다.
bin함수를 사용하면 앞에 이진수임을 나타내는 "0b"가 붙으므로 이를 replace를 이용해 제거한다.
이진수를 다 이어붙인 뒤, int(ans, 2)를 이용해 10진수로 바꾼 후, $10^9+7$로 나눈 나머지를 리턴한다.
코드
class Solution:
    def concatenatedBinary(self, n: int) -> int:
        ans = ''
        
        for i in range(1,n+1):
            ans += bin(i).replace("0b", "")
        
        return int(ans,2)%(10**9+7)728x90
    
    
  반응형
    
    
    
  '스터디 > 코테' 카테고리의 다른 글
| [LeetCode] 838 | Push Dominoes | Python (0) | 2022.09.27 | 
|---|---|
| [LeetCode] 990 | Satisfiability of Equality Equations | Python (0) | 2022.09.26 | 
| [LeetCode] 557 | Reverse Words in a String III | Python (0) | 2022.09.22 | 
| [LeetCode] 985 | Sum of Even Numbers After Queries | Python (0) | 2022.09.21 | 
| [LeetCode] 718 | Maximum Length of Repeated Subarray | Python (0) | 2022.09.20 | 
			  Comments