Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- 파이썬
- leetcode
- PYTHON
- Docker
- 코로나19
- 캐치카페
- 우분투
- ubuntu
- Baekjoon
- SW Expert Academy
- dacon
- 프로그래머스
- 프로그래머스 파이썬
- gs25
- 데이콘
- ChatGPT
- programmers
- Kaggle
- hackerrank
- 편스토랑 우승상품
- 맥북
- 금융문자분석경진대회
- 자연어처리
- 더현대서울 맛집
- 편스토랑
- 백준
- github
- Real or Not? NLP with Disaster Tweets
- Git
- AI 경진대회
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 476. Number Complement (Python) 본문
728x90
반응형
Given a positive integer num, output its complement number. The complement strategy is to flip the bits of its binary representation.
Example 1:
Input: num = 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
Example 2:
Input: num = 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
Constraints:
- The given integer num is guaranteed to fit within the range of a 32-bit signed integer.
- num >= 1
- You could assume no leading zero bit in the integer’s binary representation.
- This question is the same as 1009: https://leetcode.com/problems/complement-of-base-10-integer/
Solution
class Solution:
def findComplement(self, num: int) -> int:
return int("".join([str(1-int(num)) for num in bin(num)[2:]]), 2)
Solution 풀이
먼저 입력받은 num을 bin을 활용하여 이진수로 변경하고 변경한 다음에 3번째 인자부터 남겨둡니다.
그렇게 만든 숫자에서 한자리씩 가져와 1을 뺀 숫자를 남겨두고 이를 다시 int를 활용해 10진수로 변환해줍니다.
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 941. Valid Mountain Array (Python) (0) | 2021.01.29 |
---|---|
[leetCode] 1486. XOR Operation in an Array (Python) (0) | 2021.01.28 |
[leetCode] 784. Letter Case Permutation (Python) (0) | 2021.01.26 |
[leetCode] 367. Valid Perfect Square (Python) (0) | 2021.01.25 |
[leetCode] 804. Unique Morse Code Words (Python) (0) | 2021.01.24 |
Comments