Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions reverse-bits/freemjstudio.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

reverse-bits/freemjstudio.py
class Solution:
    def reverseBits(self, n: int) -> int:
        # 1. convert integer into binary
        binary = bin(n)[2:]

        # 2. convert into 32bits
        fill_zero = 32 - len(binary)
        binary = "0" * fill_zero + binary

        # 3. reverse the binary
        reversed_binary = binary[::-1]

        # 4. convert binary into integer
        return int(reversed_binary,2)
  • 패턴: Bit Manipulation, Divide and Conquer
  • 설명: 주어진 코드는 이진수 표현을 다루며 비트 반전을 수행하는 방식으로, 비트 조작의 아이디어를 직접적으로 사용한다. 또한 입력을 32비트로 고정시키고 역순으로 재배치하는 과정에서 분할 없이 문자열 조작으로 반전하는 방식을 보여준다.

📊 시간/공간 복잡도 분석

복잡도
Time O(1)
Space O(1)

피드백: 정수의 이진 표현을 문자열로 다루고 왼쪽 패딩과 반전을 통해 역순 이진수를 얻는다.

개선 제안: 현재 구현은 직관적이지만, 비트 연산만으로 32비트 반전을 수행하면 더 빠르고 메모리 효율적이다. 예: 비트 마스크와 쉬프트를 이용한 역순 연산으로 O(1) 시간 복잡도를 유지할 수 있다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def reverseBits(self, n: int) -> int:
# 1. convert integer into binary
binary = bin(n)[2:]

# 2. convert into 32bits
fill_zero = 32 - len(binary)
binary = "0" * fill_zero + binary

# 3. reverse the binary
reversed_binary = binary[::-1]

# 4. convert binary into integer
return int(reversed_binary,2)
Comment on lines +3 to +14

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문자열 조작 없이 비트 연산과 같은 방식 등으로 풀어보셔도 좋을 거 같습니다.

Loading