일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- BOJ
- type
- TypeScript
- dotenv
- property
- vue.js
- webpack
- bash
- nginx
- git
- 앙상블
- machine learning
- loop
- condition
- scss
- 보안
- Clone
- var
- security
- vuetify
- docker
- JavaScript
- Python
- VUE
- AI
- generic
- C++
- C#
- leetcode
- npm
Archives
- Today
- Total
ice rabbit programming
[LeetCode] 190. Reverse Bits 본문
leetcode.com/problems/reverse-bits/
32비트로 고정된 길이의 input을 받아 뒤집어 10진수로 변환하는 문제이다. 파이썬의 내장 함수인 bin()를 사용하면, 이진수로 바꿀 수 있다. 예를들어 4는 0b100 으로 변환된다. 앞에 0b가 붙는 것과, 32비트로 고정된 길이지만 앞의 padding용 0은 지워진다는 점을 이용해, 변환 후 앞 2문자를 제거하고 길이가 32가 될 때까지 0을 붙였다.
이는 string 형태이기 때문에 다시 2진수로 바꾸고, int로 바꾸어준다.
class Solution:
def reverseBits(self, n: int) -> int:
bin_n = bin(n)[2:]
while len(bin_n) < 32:
bin_n = '0'+bin_n
return int('0b' + bin_n[::-1], 2)
'PS > LeetCode' 카테고리의 다른 글
[LeetCode] 217. Contains Duplocate (0) | 2020.12.08 |
---|---|
[LeetCode] 191. Numbers of 1 Bits (0) | 2020.12.08 |
[LeetCode] 389. Find the Difference (0) | 2020.12.08 |
[LeetCode] 448. Find All Numbers Disappeared in an Array (0) | 2020.09.26 |
[LeetCode] Move Zeroes (0) | 2020.09.06 |